AcWing 237. 程序自动分析

 

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int f[N*2],a[N],b[N],c[N],n,t,p[N*2],cnt;
int find(int x)
{
    if(x!=f[x])f[x]=find(f[x]);
    return f[x];
}
int main()
{
    cin>>t;
    while(t--)
    {
        int flag=1;
        cin>>n;
        cnt=0;
        for(int i=1;i<=2*n;i++)f[i]=i;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
            p[++cnt]=a[i];p[++cnt]=b[i];
        }
        sort(p+1,p+cnt+1);
        cnt=unique(p+1,p+cnt+1)-(p+1);
        for(int i=1;i<=n;i++)
          if(c[i]){
              int x=lower_bound(p+1,p+cnt+1,a[i])-p;
              int y=lower_bound(p+1,p+cnt+1,b[i])-p;
              int fx=find(x),fy=find(y);
              if(fx==fy)continue;
              f[fx]=fy;
            }
        for(int i=1;i<=n;i++)
          if(!c[i])
          {
              int x=lower_bound(p+1,p+cnt+1,a[i])-p;
              int y=lower_bound(p+1,p+cnt+1,b[i])-p;
              int fx=find(x),fy=find(y);
              if(fx==fy)flag=0;
            }
        if(flag)puts("YES");
        else puts("NO");
    }
    return 0;
}

 

posted @ 2021-01-31 16:32  君与  阅读(49)  评论(0编辑  收藏  举报