Food chains
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 65534 |
|
Accepted: 19321 |
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 different ways: the first is "1 x y", which means 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 are in conflict with some of the previous words, 2) The current words of x or y are larger than N, which is a lie; 3) The current words mean x to eat 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 |
The crux of the problem is not only a category, but there are three categories, to find a maintenance of the three-class relationship of the method is good to do.
1#include <iostream>2#include <cstdio>3 #defineMax_n 150000+54 5 using namespacestd;6 7 intPar[max_n];//parent Node8 intDepth[max_n];//Depth9 Ten voidInitintN) { One for(intI=0; i<=n;i++){ Apar[i]=i; -depth[i]=1; - } the } - intFind_father (intt) { - if(t==Par[t]) { - returnT; +}Else{ - returnpar[t]=Find_father (par[t]); + //implementation of PATH compression A } at } - voidUniteintT1,intT2) { - intf1=find_father (t1); - intF2=Find_father (T2); - if(f1==F2) { - return ; in } - if(depth[f1]<Depth[f2]) { topar[f1]=F2; +}Else{ -par[f2]=F1; the if(depth[f1]==Depth[f2]) { *depth[f1]++; $ //Record DepthPanax Notoginseng } - } the } + A BOOLSame (intXinty) { the returnFind_father (x) = =Find_father (y); + } - $ intMain () $ { - intn,k; - intd,x,y; the intans=0; -scanf"%d%d",&n,&k);WuyiInit (n3); the for(intI=0; i<k;i++){ -scanf" %d%d%d",&d,&x,&y); Wu if(x<=0|| x>n| | y<=0|| y>n| | d==2&&x==y) { -ans++; About Continue; $ } - if(d==1){ - if(Same (x,y+n) | | Same (x,y+2*N)) { -ans++; A Continue; +}Else{ the Unite (x, y); -Unite (x+n,y+n); $Unite (x+2*n,y+2*n); the } the}Else{ the if(Same (x, y) | | Same (x,y+2*N)) { theans++; - Continue; in}Else{ theUnite (x,y+n); theUnite (x+n,y+2*n); AboutUnite (x+2*n,y); the } the } the } +printf"%d", ans); - return 0; the}
poj1182 food Chain _ and Search Set _ Challenge Program Design Contest examples