4195: [Noi2015] program automatic analysis
Description
In the process of automatic analysis of the program, it is often necessary to determine whether some constraints can be satisfied at the same time.
consider a simplified version of a constraint satisfaction problem: Suppose x1,x2,x3,... Represents the variables that appear in the program, given n shapes such as XI=XJ or XI≠XJ variables equal/unequal constraints, determine whether each variable can be given the appropriate value, so that all the above constraints are satisfied at the same time. For example, the constraint in a problem is: x1=x2,x2=x3,x3=x4,x1≠x4, these constraints are obviously impossible to be satisfied at the same time, so this problem should be judged not to be satisfied. now give some constraints to satisfy the problem, please judge them separately.
Input
The 1th line of the input file contains 1 positive integer t, which indicates the number of questions that need to be determined. Note that these problems are independent of each other.
for each question, include several lines:The 1th line contains 1 positive integer n, which indicates the number of constraints that need to be met in the problem. The next n rows, each line consisting of 3 integer i,j,e, describe 1 equal/unequal constraints, separated by a single space between adjacent integers. If e=1, the constraint is xi=xj; if e=0, the constraint is XI≠XJ.
Output
The output file includes a T line.
The output file of the K line output a string "yes" or "no" (without quotation marks, all uppercase letters), "YES" indicates that the input of the K problem is determined to be satisfied, "no" means not to be satisfied.
Sample Input
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
Sample Output
NO
YES
HINT
In the first question, the constraint is: x1=x2,x1≠x2. These two constraints contradict each other and therefore cannot be satisfied at the same time.
in the second question, the constraint is: x1=x2,x2=x1. These two constraints are equivalent and can be satisfied at the same time. 1≤n≤10000001≤i,j≤1000000000The following:Yes and check the set no doubt. P.S. First thought wrong, to the classic friend and enemy aspects of thinking, but this is not equal to make a mess .... in fact, as long as the first maintenance equals the relationship is good ....
#include <stdio.h>#include<iostream>#include<algorithm>using namespacestd;Const intn=1000005;structnode{intA,id;} P[n<<1];intt,n,i,p,k,a[n],h[n<<1],f[n<<1];inlinevoidReadint&v) { Charch,fu=0; for(ch='*'; (ch<'0'|| Ch>'9') &&ch!='-'; Ch=GetChar ()); if(ch=='-') fu=1, ch=GetChar (); for(v=0; ch>='0'&&ch<='9'; Ch=getchar ()) v=v*Ten+ch-'0'; if(FU) v=-v;}BOOLcmpConstNode&x,Constnode&y) { returnx.a<y.a;}int Get(intx) { if(f[x]==x)returnXElse returnf[x]=Get(F[x]);}intMain () {read (T); while(t--) {read (n); for(i=1; i<=n;i++) {Read (P[i*2-1].A), read (p[i*2].a), read (A[i]); P[i*2-1].id=i*2-1, p[i*2].id=i*2; } sort (P+1, p+n*2+1, CMP); K=0; for(i=1; i<=n*2; i++) { if(p[i].a!=p[i-1].A) k++; H[p[i].id]=K; F[i]=i; } for(i=1; i<=n;i++) if(a[i]==1) { intfx=Get(h[i*2-1]), fy=Get(h[i*2]); if(fx!=fy) f[fx]=fy; } P=0; for(i=1; i<=n;i++) if(a[i]==0) { intfx=Get(h[i*2-1]), fy=Get(h[i*2]); if(FX==FY) {p=1; Break;} } if(p==1) printf ("no\n");Elseprintf"yes\n"); } return 0;}
Bzoj 4195: [Noi2015] Program automatic analysis