[BZOJ4195] [NOI2015] 程序自动分析 - 并查集 + 离散化

来源:互联网 发布:域名注册商和持有者 编辑:程序博客网 时间:2024/04/30 05:42

辣鸡题啊QAQ然而我交了好几次

#include "algorithm" #include "iostream"#include "stdio.h"#define rep(f,a,b) for(f=a;f<=b;f++)using namespace std;const int N=1000005;inline int read(){int v=0; char ch=getchar();while (ch<'0'||ch>'9') ch=getchar();while (ch<='9'&&ch>='0') { v=v*10+ch-'0'; ch=getchar();}return v;}int fa[N],n,a[N],b[N],e[N],c[2*N];int find(int x){if(fa[x]==x) return x;return fa[x]=find(fa[x]);}void work(){ n=read(); int i;rep(i,1,2*n) fa[i]=i;rep(i,1,n) {c[2*i-1]=a[i]=read();        c[2*i]=b[i]=read();e[i]=read(); } int cl=0; i=1;sort(c+1,c+2*n+1);while (i<=2*n){ c[++cl]=c[i++];while (c[i]==c[i-1]&&++i<=2*n);} int bot,top,mid;rep(i,1,n){ bot=1,top=cl;while (mid=(bot+top+1)>>1,bot<top){c[mid]>a[i]?top=mid-1:bot=mid;} mid=(bot+top)>>1;        a[i]=mid; bot=1,top=cl;while (mid=(bot+top+1)>>1,bot<top){c[mid]>b[i]?top=mid-1:bot=mid;} mid=(bot+top)>>1;        b[i]=mid;} int x,y;rep(i,1,n){x=find(a[i]),y=find(b[i]);if (x!=y && e[i]) fa[x]=y;}rep(i,1,n){x=find(a[i]),y=find(b[i]);if (x==y && !e[i]){ puts("NO"); break;}    }if(i>n) puts("YES");}int main(){int T; T=read();while(T--) work();return 0;}


0 0