Food chains
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 60260 |
|
Accepted: 17665 |
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
Source
Noi 01
Feel and look at a very tall look ah but actually we think it is very simple in fact, the main processing of the relationship between the parent-child node and then the relationship between the child node and the root node of the path compression, even if it is to solve the problem ...
At that time, the POJ, or the Chinese ... Moved......
Code in this:
#include <iostream> #include <cstdio>using namespace std;int father[50001],rank[50001];int n,k;int flag=0; void init (int n)//Initialize {for (int i=1;i<=n;++i) {father[i]=i; rank[i]=0; }}int getanc (int x) {if (x==father[x]) return x; itself else {int Fx=getanc (father[x]); Find root node of this point rank[x]= (Rank[x]+rank[father[x]])%3;//determine the relationship between this point and the root node eating and being eaten father[x]=fx;//change this point parent node to root node, path compression } return father[x];} BOOL Union_set (int x,int y,int d) {int fx=getanc (x);//two number root node int fy=getanc (y); if (fx==fy) {if (rank[y]-rank[x]+3)%3== (d-1)) return true;//If the two number root nodes are the same and the corresponding relationship is correct, the truth else return FAL Se } father[fy]=fx; Rank[fy]= ((3-rank[y]) + (Rank[x]) + (d-1))%3;//rank[fy] inductive, two-digit root node correspondence, three parts return true; int D,x,y;int Main () {scanf ("%d%d", &n,&k); Init (n); flag=0; for (int i=0;i<k;++i) {scanf ("%d%d%d", &d,&x,&y); if ((d==2&&x==y) | | | X>n| | Y>n) {flag++;continue;} else if (!union_set (x,y,d)) flag++; } printf ("%d\n", flag); return 0;}
poj1182 food chain (with right and check set + path compression)