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.
and check Set
Merges the known equality i,j into the same set, judging whether the i,j of each unequal condition belongs to the same set, and if it is not satisfied
#include <cstdio>#include<algorithm>inlineintRead () {intx=0, c=GetChar (); while(c>'9'|| c<'0') c=GetChar (); while(c>='0'&&c<='9') x=x*Ten+c-'0', c=GetChar (); returnx;}intT,n,now;intueq[1000005][2],up=0;inteq[1000005][2],ep=0;intvs[2000005],vp=0;intvals[2000005];inlineint Get(intx) { returnStd::upper_bound (vs,vs+vp,x)-vs;}intFindintx) { returnvals[x]==x?x:vals[x]=find (Vals[x]);}intMain () {intA,b,c; T=read (); while(t--) {n=read (); VP=ep=up=0; for(intj=0; j<n;j++) {a=read (); b=read (); C=read (); VS[VP++]=A; VS[VP++]=b; if(c) eq[ep][0]=a,eq[ep++][1]=b; Elseueq[up][0]=a,ueq[up++][1]=b; } std::sort (Vs,vs+VP); intvp1=VP; VP=1; for(intI=1; i<vp1;i++)if(vs[i]!=vs[i-1]) vs[vp++]=Vs[i]; for(intI=0; i<=vp;i++) vals[i]=i; BOOLyes=1; for(intI=0; i<ep;i++) Vals[find (Get(eq[i][0]))]=find (Get(eq[i][1])); for(intI=0; i<up;i++){ if(Find (Get(ueq[i][0])) ==find (Get(ueq[i][1])) {Yes=0; Break; }} puts (yes?"YES":"NO"); } return 0;}
Automatic analysis of bzoj4195 program