1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int Mn(1000050); int fa[Mn*2]; int ff(int x) { return fa[x] ? (fa[x] = ff(fa[x])) : x; } int merge(int x,int y) { if(x!=y) { fa[y] = x; } } int a[Mn],b[Mn],c[Mn]; int arr[Mn*2];
int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i(1);i<=n;++i) { scanf("%d %d %d",a+i,b+i,c+i); arr[i*2-1] = a[i]; arr[i*2] = b[i]; } sort(arr+1,arr+2*n+1); int tn = unique(arr+1,arr+2*n+1) - arr - 1; memset(fa,0,sizeof fa); bool isa(true); for(int i(1);i<=n;++i) { if(c[i]==1) { int x = lower_bound(arr+1,arr+tn+1,a[i]) - arr; int y = lower_bound(arr+1,arr+tn+1,b[i]) - arr; int rx = ff(x), ry = ff(y); merge(rx,ry); } } for(int i(1);i<=n;++i) { if(c[i]==0) { int x = lower_bound(arr+1,arr+tn+1,a[i]) - arr; int y = lower_bound(arr+1,arr+tn+1,b[i]) - arr; int rx = ff(x), ry = ff(y); if(rx==ry) { isa = false; break; } } } printf("%s\n",isa?"YES":"NO"); } return 0; }
|