Food chains
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 65430 |
|
Accepted: 19283 |
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
A different kind of relationship and look up the set
V[i] 0 and root homogeneous 1 eat root 2 eaten by root
You can find 1->0->2->1 .....
Can think from a vector point of view, such as X->y +3%3 is 1 words x eat y
V[X] is x->fa[x] The value of this vector
The discovery of D-1 exactly describes the relationship of the x->y vector.
When the path is compressed and merged, all the drawings are pushed with a vector.
Another approach is to create 3 sets for each animal x: X for Animals of the same type as x, x+n for x to eat, x+2*n for animals that eat x.
Some understanding
And the search set is to maintain a number of relations
Types and collections are merging things that know the relationship, by assigning a value to handle
Another approach is to merge the same class
////main.cpp//poj1182////Created by Candy on 31/10/2016.//Copyright Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn=5e4+5; inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,c,x,y,ans=0;intFa[n],v[n];inlineintFindintx) { if(X==fa[x])returnx; introot=find (fa[x]); V[X]= (V[x]+v[fa[x]])%3;//X->fa[x] + fa[x]->root returnfa[x]=Root;} InlineintUnn (intXintYintop) { intF1=find (x), f2=find (y); if(f1==F2) { if((-v[y]+v[x]+3)%3!=OP)return 1; }Else{FA[F1]=F2; V[F1]= (op+v[y]-v[x]+3)%3; } return 0;}intMainintargcConst Char*argv[]) {N=read (); m=read (); for(intI=1; i<=n;i++) fa[i]=i,v[i]=0; for(intI=1; i<=m;i++) {C=read (); X=read (); y=read (); if(x>n| | y>n| | (c==2&&x==y)) {ans++;Continue;} Ans+=unn (x,y,c-1); } printf ("%d", ans); return 0;}
from the Luogu solution, the other approach to the problem is obviously to use and check the set. Because there are only 3 species of animals, my method is to set up 3 sets for each animal x: x represents an animal of the same type as x, x+n says to eat x animals, x+2*n means an animal eating x. For each read-in description d X y, do the following: if X or y no longer range [1, n], this is a lie. D is 1 if x+n or x+2*N and y are described in the same set that x and Y are not the same animal, which is false; otherwise, x and y,x are respectively+n and y+n,x+2*n and y+2*n Merge. D is 2 if X and Y are in the same set, it is known that X and Y are the same animal, which is a false statement; if X+2*N and y in the same set, that is known as Y eat x, this sentence is false; otherwise, x and Y are respectively+2*n,x+n and y,x+2*n and y+n Merge. It is very complicated to say, it is very simple to implement, see the code below: #include<iostream>#include<algorithm>using namespacestd;intp[150001];intFind (intx) {returnx = = P[x]? X:P[X] =Find (p[x]);}voidInit (intN) { for(inti =1; I <=3N i++) P[i] =i;}voidUnion (intXinty) { intxx = Find (x), yy =Find (y); if(xx! = yy) P[xx] =yy;}intMain () {intN, k, ans =0; CIN>> N >>K; Init (n); for(inti =1; I <= K; i++) { intA, x, y; CIN>> a >> x >>y; if(X > N | | y > N | | x <1|| Y <1) {ans++;Continue; } if(A = =1) { if(Find (x + N) = = Find (y) | | Find (x +2* N) = = = Find (y)) {ans++;Continue; } Union (x, y); Union (x+ N, y +N); Union (x+2* N, y +2*N); } Else { if(Find (x) = = Find (y) | | Find (x +2* N) = = = Find (y)) {ans++;Continue; } Union (x, y+2*N); Union (x+n, y); Union (x+2* N, y +N); }} cout<<ans;}
noi2001| POJ1182 food chain [species and check set vector]