题面
https://www.luogu.org/problem/P1955
题解
#include<cstdio> #include<iostream> #include<algorithm> #define ri register int using namespace std; int f[200050]; int a[100050],b[100050],c[100050]; struct node { int x,id,s; bool operator < (const node &rhs) const { return x<rhs.x; } } ll[200050]; int findroot(int x){ if (f[x]==x) return x; return f[x]=findroot(f[x]); } int n,T; int main(){ scanf("%d",&T); while (T--) { scanf("%d",&n); for (ri i=1;i<=n;i++) { scanf("%d %d %d",&a[i],&b[i],&c[i]); ll[2*i-1]=(node){a[i],i,0}; ll[2*i]=(node){b[i],i,1}; } sort(ll+1,ll+2*n+1); int cnt=0; for (ri i=1;i<=2*n;i++) { if (ll[i].x!=ll[i-1].x) cnt++; if (ll[i].s==0) a[ll[i].id]=cnt; else b[ll[i].id]=cnt; } for (ri i=1;i<=cnt;i++) f[i]=i; for (ri i=1;i<=n;i++) if (c[i]==1) { int r1=findroot(a[i]),r2=findroot(b[i]); if (r1==r2) continue; f[r1]=r2; } int ans=1; for (ri i=1;i<=n;i++) if (c[i]==0) { int r1=findroot(a[i]),r2=findroot(b[i]); if (r1==r2) {ans=0;break;} } if (ans) puts("YES"); else puts("NO"); } }