POJ 1182 food chain && Nyoj 207 (with right and check set)

Source: Internet
Author: User

Food chains
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 52414 Accepted: 15346

Description

There are three types of animal a,b,c in the animal kingdom, and the food chain of these three animals is an interesting ring. A eat B, B eat c,c eat a.
Existing n animals, numbered with 1-n. Every animal is one of the a,b,c, but we don't know what it is.
Some people describe the food chain relationship between these n animals in two ways:
The first argument is "1 x y", which means that x and Y are homogeneous.
The second argument is "2 x y", which means x eats y.
This person to n animals, with the above two statements, a sentence after sentence to say K sentence, this k sentence some is true, some false. When one sentence satisfies one of the following three, the sentence is a lie, otherwise it is the truth.
1) The current words conflict with some of the preceding words, which is false;
2) The current word in x or y is greater than N, is a lie;
3) The current words say x eats x, is a lie.
Your task is to output the total number of falsehoods according to the given N (1 <= n <= 50,000) and the K-sentence (0 <= K <= 100,000).

Input

The first line is two integers n and K, separated by a single space.
The following k lines each line is three positive integer d,x,y, and two numbers are separated by a space, where D denotes the kind of claim.
If d=1, it means that x and Y are homogeneous.
If d=2, it means x eats y.

Output

There is only one integer that represents the number of false lies.

Sample Input

100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5

Sample Output

3

Test instructions: There are a, B, c Three species of animals, a eat b,b eat c,c eat A, and give some conditions, judge for you to provide information about how many words of falsehood.
And check sets, which are commonly used to check whether two elements are in the same set, or two different collections as a collection.
As for this problem, there is the man of God, using and checking the set, at the same time, each node maintains its relative class offset to the root node, defined as:
0--similar;
food;
2--predators.
So we also need to know how to find the relative class offset.
For the input relationship D x y:
1). If x, y belong to the same set, determine whether the relationship between the two is reasonable:
X, Y is the same set, so X, Y points to the same root. The relationship between x and the root RT is dist[x], the relationship between Y and the root RT is dist[y], and the distance from the input x to Y is D. There are d+ dist[y]== dist[x] (mod 3).


2). If x, Y does not belong to the same set, there are:
X and Rx belong to the same set, Rx is the root of x, Y is the same set as Ry, and Ry is the root of Y.
rx!= ry, x and y do not belong to the same set, then X, y should be merged, where the RX points to Ry when merging,
Ry becomes the root of the new collection: there is
d+ dist[y]== dist[x]+ Dist[rx]--dist[rx]= d+ dist[y]-dist[x] Find the relationship distance.


Another problem: After merging X with Y, Ry becomes the root of the new collection, and the relationship between the nodes in the set with the RX root and the RY needs to be re-determined, and how to determine:
Assuming that the set tree is X->a->b->rx->ry, the relationship distance of Rx->ry has been calculated, that is, Dist[rx] is known, the relationship between B to Ry Distance is b to Rx distance plus rx to ry distance and.
So dist[b]= dist[b]+ Dist[rx]. To find out in turn.
That is: For a set of data D x y, if X is the same as the root of Y,
The use of (dist[x]-dist[y]+3)%3!=d-1 if not equal to the description of falsehood;
If x is different from the Y root, it means that the two are not grouped together.
Let Y's root fy become the parent of the root FX of X (FATHER[FX]=FY), the value of dist[fx] needs to be carefully summed up.
2015,7,27
This input is scanf (). And Cin was a tle.

A very detailed report of the problem-solving, see me pleasantly surprised: http://blog.csdn.net/c0de4fun/article/details/7318642/

#include <stdio.h> #define M 50005int x[m],re[m];void init () {for (int i=0;i<m;i++) {x[i]=i; re[i]=0;}} int find (int k) {int temp=x[k];if (X[K]==K) return K;x[k]=find (X[k]), re[k]= (Re[k]+re[temp])%3;//You can verify by drawing a diagram, similar to the addition of vectors, I don't know exactly how to push it. return x[k];} void merge (int a,int b,int fa,int fb,int d) {x[fa]=fb;re[fa]= (re[b]-re[a]+d+3)%3;//here is R[b]-r[a]}int main () {int n,m,a,b, D,FA,FB,COUNT=0;SCANF ("%d%d", &n,&m); init (); while (m--) {scanf ("%d%d%d", &d,&a,&b); if (A>n | | B>n | | (a==b && d==2)) {count++;continue;} Fa=find (a); Fb=find (b); if (fa==fb&& (re[a]-re[b]+3)%3!=d-1)//Here is r[a]-r[b] with the above function inside the difference can also draw a picture to prove, specifically why, Forgive me for being too vegetable count++;elsemerge (a,b,fa,fb,d-1);} printf ("%d\n", count); return 0;}


POJ 1182 food chain && Nyoj 207 (with right and check set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.