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
Relationship and check set;
#include <cstdio> #include <cstring>using namespace Std;int n,d,sum;int father[50010],relx[50010];int find (int x) { int rx; if (X!=father[x]) { rx=find (father[x]); Relx[x]= (Relx[x]+relx[father[x]])%3; Father[x]=rx; } return father[x];} int un (int x,int y) { int rx=find (x), Ry=find (y); if (Rx==ry) { if ((relx[y]-relx[x]+3)%3==d-1) return 0; return 1; } Father[ry]=rx; relx[ry]= (relx[x]-relx[y]+d+2)%3; return 0;} int main () { int k,x,y,i; sum=0; scanf ("%d%d", &n,&k); for (i=1;i<=n;i++) {father[i]=i;relx[i]=0;} while (k--) { scanf ("%d%d%d", &d,&x,&y); if (x>n| | y>n| | (x==y&&d==2)) {sum++;continue;} if (UN (x, y) ==1) sum++; } printf ("%d\n", sum);}
POJ-1182 food Chain