标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 207 Accepted Submission(s): 91
1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 #include<iostream> 5 #include<queue> 6 #include<stdlib.h> 7 #include<math.h> 8 #include<set> 9 using namespace std; 10 int bin[100005]; 11 int cnt[100005]; 12 int du[100005]; 13 int ans[100005]; 14 set<int>que; 15 int main(void) 16 { 17 int n; 18 scanf("%d",&n); 19 while(n--) 20 { 21 que.clear(); 22 int i,j; 23 memset(cnt,0,sizeof(cnt)); 24 for(i = 0; i <= 100005; i++) 25 { 26 bin[i] = i; 27 du[i] = 1; 28 } 29 int N,M; 30 scanf("%d %d",&N,&M); 31 if(M==0)printf("0\n"); 32 else 33 { 34 for(i = 1; i <= N; i++) 35 { 36 scanf("%d",&ans[i]); 37 } 38 while(M--) 39 { 40 int x,y; 41 scanf("%d %d",&x,&y); 42 cnt[x]++; 43 cnt[y]++; 44 int xx,yy; 45 for(xx = x; bin[xx]!=xx;) 46 xx = bin[xx]; 47 for(yy = y; bin[yy]!=yy;) 48 yy = bin[yy]; 49 if(xx != yy) 50 { 51 if(du[xx]>du[yy]) 52 { 53 bin[yy] = xx; 54 du[xx] += du[yy]; 55 } 56 else 57 { 58 bin[xx] = yy; 59 du[yy] += du[xx]; 60 } 61 } 62 } 63 for(i = 1; i <= N; i++) 64 { 65 if(cnt[i]) 66 { 67 int xx; 68 for(xx = i; xx!=bin[xx];) 69 xx = bin[xx]; 70 que.insert(xx); 71 } 72 } 73 int cn = 0; 74 for(i = 1; i <= N; i++) 75 { 76 if(cnt[i]) 77 { 78 if(cnt[i]%2) 79 { 80 cn++; 81 } 82 } 83 } 84 int sum = 0; 85 if(cn == 1|| cn > 3||que.size()!=1) 86 { 87 //printf("1\n"); 88 printf("Impossible\n"); 89 } 90 else if(cn == 2) 91 { 92 for(i = 1; i <= N; i++) 93 { 94 if(cnt[i]>1) 95 cnt[i]=cnt[i]+1; 96 cnt[i]/=2; 97 if(cnt[i]%2) 98 sum^=ans[i]; 99 } 100 printf("%d\n",sum); 101 } 102 else 103 { 104 for(i = 1; i <= N; i++) 105 { 106 if(cnt[i]) 107 { 108 sum ^= ans[i]; 109 } 110 } 111 int flag = 0; 112 int k = sum; 113 for(i = 1; i <= N; i++) 114 { 115 if(cnt[i]) 116 { 117 if(!flag) 118 sum^=ans[i],flag = 1; 119 else sum = max(sum,k^ans[i]); 120 } 121 } 122 printf("%d\n",sum); 123 } 124 } 125 } 126 return 0; 127 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5879672.html