标签:分享 view else 并查集 puts display main ble struct
带权并查集,回去再写
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define CLR(m,a) memset(m,a,sizeof(m)) 4 const int maxn=20010; 5 int f[maxn],d[maxn]; 6 int n,q; 7 void init(){ 8 for(int i=0;i<=n;i++){ 9 f[i]=i; 10 d[i]=0; 11 } 12 } 13 14 int gf(int x){ 15 if(x!=f[x]){ 16 int r=f[x]; 17 f[x]=gf(r); 18 d[x]=d[x]^d[r]; 19 } 20 return f[x]; 21 } 22 int uni(int a,int b,int v){ 23 int pa=gf(a); 24 int pb=gf(b); 25 if(pa==pb) return (d[a]^d[b])==v; 26 if(pa==n) swap(pa,pb); 27 f[pa]=pb; 28 d[pa]=d[a]^d[b]^v; 29 return 1; 30 } 31 32 struct Ask{ 33 int p,a; 34 bool operator<(const Ask& x){ 35 return p<x.p; 36 } 37 }; 38 Ask ask[maxn<<1]; 39 int query(int k){ 40 for(int i=0;i<k;i++) ask[i].p=gf(ask[i].a); 41 sort(ask,ask+k); 42 int l=0,ans=0; 43 while(l<k){ 44 int r=l; 45 while(r+1<k&&ask[r].p==ask[r+1].p) r++; 46 int num=r-l+1; 47 if(ask[l].p!=n&&num&1) return -1; 48 for(int i=l;i<=r;i++) ans^=d[ask[i].a]; 49 l=r+1; 50 } 51 return ans; 52 } 53 int main() 54 { 55 int kase=0; 56 char c[5],s[20]; 57 while(scanf("%d%d",&n,&q)&&(n||q)){ 58 printf("Case %d:\n",++kase); 59 int flag=-1; 60 init(); 61 int ct=0; 62 for(int u=0;u<q;u++){ 63 scanf("%s",c); 64 if(c[0]==‘I‘){ 65 ct++; 66 gets(s); 67 if(flag!=-1) continue; 68 int p,q,v; 69 int res; 70 int t=sscanf(s,"%d%d%d",&p,&q,&v); 71 if(t==2){ 72 res=uni(p,n,q); 73 }else{ 74 res=uni(p,q,v); 75 } 76 if(!res) flag=ct; 77 }else { 78 int k; 79 scanf("%d",&k); 80 for(int i=0;i<k;i++) { 81 scanf("%d",&ask[i].a); 82 } 83 if(flag!=-1) continue; 84 int res=query(k); 85 if(res==-1) puts("I don‘t know."); 86 else printf("%d\n",res); 87 } 88 } 89 if(flag!=-1) printf("The first %d facts are conflicting.\n",flag); 90 puts(""); 91 } 92 return 0; 93 }
标签:分享 view else 并查集 puts display main ble struct
原文地址:http://www.cnblogs.com/yijiull/p/7264308.html