Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 18 Accepted Submission(s): 8
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int N = 100005; int a[N]; int father[N]; int degree[N]; int _find(int x) { if(x!=father[x]) father[x] = _find(father[x]); return father[x]; } void Union(int a,int b) { int x = _find(a); int y = _find(b); if(x==y) return ; father[x] = y; } void init(int n) { for(int i=1; i<=n; i++) { father[i] = i; degree[i] = 0; } } int main() { int tcase,n,m; scanf("%d",&tcase); while(tcase--) { scanf("%d%d",&n,&m); init(n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); } for(int i=1; i<=m; i++) { int u,v; scanf("%d%d",&u,&v); degree[u]++; degree[v]++; Union(u,v); } int ans = 0; for(int i=1; i<=n; i++) { if(degree[i]>0) { if(_find(i)==i) ans++; } } ///连通分量个数 if(ans>1) { printf("Impossible\n"); continue; } ans = 0; for(int i=1; i<=n; i++) { if(degree[i]%2!=0) ans++; } ///判断是否为欧拉路 if(ans!=0&&ans!=2) { printf("Impossible\n"); continue; } LL res = 0; if(ans==0) { for(int i=1; i<=n; i++) { int K = degree[i]/2; if(degree[i]!=0&&K%2==1){ res = res^a[i]; } } LL MAX = -1; for(int i=1; i<=n; i++) { int K = degree[i]/2; if(degree[i]!=0){ MAX = max(MAX,res^a[i]); } } res = MAX; } else { for(int i=1; i<=n; i++) { int K = degree[i]/2; if(degree[i]%2==0&&K%2==1) { res = res^a[i]; } if(degree[i]%2==1) { K = (degree[i]+1)/2; if(K%2==1) res = res^a[i]; } } } printf("%lld\n",res); } return 0; }
原文:http://www.cnblogs.com/liyinggang/p/5879335.html