bzoj 4195程序自动分析
先离散一下,然后并查集就好了。
(一开始作大死,没全离散,WA一片)
1 #include<bits/stdc++.h> 2 #define INF 0x7fffffff 3 #define LL long long 4 #define N 1000005 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 int fa[N],b[N],n,m,cnt,tot,sum; 14 bool flag; 15 struct node{ 16 int x,y; 17 }ask[N],a[N]; 18 int find(int x) 19 { 20 return fa[x]==x?x:fa[x]=find(fa[x]); 21 } 22 int main() 23 { 24 int T=ra(); 25 while (T--) 26 { 27 map<int ,int > mp; sum=0; 28 n=ra(); cnt=0; tot=0; flag=0; 29 for (int i=1; i<=n; i++) 30 { 31 int x=ra(),y=ra(),z=ra(); 32 if (z==0) 33 { 34 b[++sum]=x; b[++sum]=y; 35 ask[++cnt].x=x,ask[cnt].y=y; 36 } 37 else { 38 a[++tot].x=x; a[tot].y=y; 39 b[++sum]=x; b[++sum]=y; 40 } 41 } 42 sort(b+1,b+sum+1); 43 int len=unique(b+1,b+sum+1)-b; 44 unique(b+1,b+sum+1); 45 for (int i=1; i<len; i++) 46 mp[b[i]]=i; 47 for (int i=1; i<=sum; i++) fa[i]=i; 48 for (int i=1; i<=tot; i++) 49 { 50 int q=find(mp[a[i].x]),p=find(mp[a[i].y]); 51 if (q!=p) 52 fa[p]=q; 53 } 54 for (int i=1; i<=cnt; i++) 55 { 56 int q=find(mp[ask[i].x]),p=find(mp[ask[i].y]); 57 if (p==q) 58 { 59 cout<<"NO"<<endl; 60 flag=1; 61 break; 62 } 63 } 64 if (!flag) cout<<"YES"<<endl; 65 } 66 return 0; 67 }