首页 > 代码库 > The Best Path

The Best Path

The Best Path

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 207    Accepted Submission(s): 91


Problem Description
Alice is planning her travel route in a beautiful valley. In this valley, there are N lakes, and M rivers linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up a path which go through every river exactly once. In addition, Alice has a specific number (a1,a2,...,an) for each lake. If the path she finds is P0P1...Pt, the lucky number of this trip would be aP0XORaP1XOR...XORaPt. She want to make this number as large as possible. Can you help her?
 

 

Input
The first line of input contains an integer t, the number of test cases. t test cases follow.

For each test case, in the first line there are two positive integers N (N100000) and M (M500000), as described above. The i-th line of the next N lines contains an integer ai(i,0ai10000) representing the number of the i-th lake.

The i-th line of the next M lines contains two integers ui and vi representing the i-th river between the ui-th lake and vi-th lake. It is possible that ui=vi.
 

 

Output
For each test cases, output the largest lucky number. If it dose not have any path, output "Impossible".
 

 

Sample Input
23 23451 22 34 312341 22 32 4
 

 

Sample Output
2Impossible
 思路:欧拉路,欧拉回路;
首先判断给定的边的点是否连通,因为要经过每条边一次,所以用欧拉路来判断,如果是欧拉路的话,那么就是原来所有边经过的点的亦或和,否则如果是欧拉回路的话那么起点会多经过一次,那么枚举起点就行了;
  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             else103             {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 }

 

The Best Path