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 P0→P1→...→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 (N≤100000) and M (M≤500000), as described above. The i-th line of the next N lines contains an integer ai(∀i,0≤ai≤10000) 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
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
 思路:欧拉路,欧拉回路;
首先判断给定的边的点是否连通,因为要经过每条边一次,所以用欧拉路来判断,如果是欧拉路的话,那么就是原来所有边经过的点的亦或和,否则如果是欧拉回路的话那么起点会多经过一次,那么枚举起点就行了;
  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 }

最新文章

  1. 数据见50条常用sql
  2. android 修改videoview的宽度和高度
  3. table中bordercolor属性设置后最新ie浏览器或firefox中不显示边线,借助table的css来实现边线
  4. 洛谷 P1031 均分纸牌 Label:续命模拟QAQ
  5. 表单元素的写法及与后台php的交互
  6. Mongodb shell 基本操作
  7. POJ3345
  8. 利用 __FUNCTION__ 宏打印函数调用信息
  9. Piggy-Bank (完全背包)
  10. ExtJs--02--MessageBox相关弹出窗口alert,prompt,confirm采用
  11. django模板系统基础
  12. j2ee的十三种技术
  13. centos6.5 修改java环境变量
  14. js中一个对象当做参数传递时候?
  15. JS中JSON和string字符串相互转换
  16. Koa 中 ejs 模板的使用
  17. vue--js里跳转页面
  18. 启用SQL Server 2014 中的OLE 自动化功能
  19. PowerBI更新 - 解决方案架构 - PowerBI Solution Architecture(一图胜万字!)
  20. c语言const和c++const

热门文章

  1. C#判断是否有中文
  2. [PE结构]导出表结构浅析
  3. C++字节对齐(对象大小)
  4. android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token)
  5. Linux学习 - 输入输出重定向,管道符,通配符
  6. oracle(查询数据库对象1)
  7. vue-cli4脚手架搭建二
  8. 【Linux】【Basis】磁盘分区
  9. mysql安装 报错解决
  10. 莫烦python教程学习笔记——利用交叉验证计算模型得分、选择模型参数