标签:pre -- ace alt mamicode ios int png ++
介绍
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define N 1000010 using namespace std; int fa[N]; int find(int x) { if(fa[x]==x) return x; int t=find(fa[x]); fa[x]=t; return t; //return fa[x]==x?x:fa[x]=find(fa[x]); } void merge(int x,int y) { x=find(x); y=find(y); if(x==y) return; fa[x]=y; } int m,x[N],y[N],f[N]; void doit() { memset(fa,0,sizeof(fa)); for(int i=1;i<=1000000;i++) fa[i]=i; cin>>m; for(int i=1;i<=m;i++) { cin>>x[i]>>y[i]>>f[i]; if(f[i]==1) merge(x[i],y[i]); } bool ans=true; for(int i=1;i<=m;i++) { if(f[i]==0) { if(find(x[i])==find(y[i])) ans=false; } } puts(ans?"YES":"NO"); } int main() { int T; cin>>T; while(T--) doit(); }
标签:pre -- ace alt mamicode ios int png ++
原文地址:https://www.cnblogs.com/liusu123456/p/12210135.html