hdu 5883

Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM 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,...,ana1,a2,...,an) for each lake. If the path she finds is P0P1...PtP0→P1→...→Pt, the lucky number of this trip would be aP0XORaP1XOR...XORaPtaP0XORaP1XOR...XORaPt. She want to make this number as large as possible. Can you help her?

InputThe first line of input contains an integer tt, the number of test cases. tt test cases follow. 

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

The ii-th line of the next MM lines contains two integers uiui and vivi representing the ii-th river between the uiui-th lake and vivi-th lake. It is possible that ui=viui=vi.
OutputFor each test cases, output the largest lucky number. If it dose not have any path, output "Impossible".Sample Input

2
3 2
3
4
5
1 2
2 3
4 3
1
2
3
4
1 2
2 3
2 4

Sample Output

2
Impossible


题意:t组数据,n个点,m条边。每个点都有权值。问这个图能不能构成欧拉通路(或回路。如果能,求从起点异或到终点的值中的最大值。

解题思路:判断下能否构成,如果能构成通路,则只有一条路径,如果能构成回路,需要枚举起点。注意异或的时候不需要重现路径,因为
a^a=0所以只要通过某个点偶数次,则这个点就不需要异或。因为在通路中起点和终点的度为奇数,所以取值时应该向上取整。如样例1中
1是起点,度数为1,通过该点的次数也为1,所以应是(nu[i]+1)/2%2才能正确判断通过该点的次数是否为奇数。
而如果是回路,则通过起点的次数要通路多一次,终点的次数不变。(不明白的话可画一个简单的例子模拟一下
所以只需要遍历所有点,取对每个点异或之后的最大值便是答案。

ac代码:
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #define ll long long
 8 using namespace std;
 9 const int maxn = 1e5+10;
10 int nu[maxn];
11 int val[maxn];
12 int main()
13 {
14     int t,n,m;
15     scanf("%d",&t);
16     while(t--)
17     {
18         memset(nu,0,sizeof(nu));
19         scanf("%d%d",&n,&m);
20         for(int i=1;i<=n;++i)
21         {
22             scanf("%d",&val[i]);
23         }
24         int u,v;
25         for(int i=1;i<=m;++i)
26         {
27             scanf("%d%d",&u,&v);
28             nu[u]++;
29             nu[v]++;
30         }
31         int cnt=0;
32         for(int i=1;i<=n;++i)
33         {
34             if(nu[i]%2==1)
35             {
36                 cnt++;
37             }
38         }
39      //   cout<<cnt<<endl;
40         if(cnt!=0 && cnt!=2)
41         {
42             printf("Impossible\n");
43             continue;
44         }
45         else
46         {
47             int ans=0;
48             for(int i=1;i<=n;++i)
49             {
50                 if((nu[i]+1)/2%2==1)
51                     ans^=val[i];
52             }
53             if(cnt==0)
54             {
55                 int u=ans;
56                 for(int i=1;i<=n;++i)
57                 {
58                     ans=max(ans,u^val[i]);
59                 }
60             }
61             printf("%d\n",ans);
62         }
63     }
64 }
View Code

 

posted @ 2017-12-09 15:40  euzmin  阅读(291)  评论(0编辑  收藏  举报