The Best Path
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
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 Nlines 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.
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 Nlines 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
题意:给你n个点,m条无向路,现在找到一条路线,经过所有路且每条路只经过一次。如果能找到,输出路线中点的值全部异或起来的值,否则输出Impossible
题解:先判掉m=0的情况,即输出num中最大的,然后判掉所有路不在一个图的情况。
对于每个点,需要异或的次数是(s+1)/2%2,s是连的边数,如果是s是奇数,多一进或者多一出,如果是偶数,则进出相等, 所以+1是多一进或者多以出的情况,%2是异或的性质,即x^x=0。
对于欧拉回路,每个点连的边数是奇数的点只可能是0个或2个。
0情况:起点终点是一个点,构成环,答案即为上面的异或起来再找个终点
举个例子 1 2 3构成环 那么有三条路线 1-2-3-1 2-3-1-2 3-1-2-3 所以选个终点异或起来ans 取最大即可
2情况:起点终点不是一个点,不用特判
其他:Impossible
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,num[100005],pre[100005],tim[100005];
int find(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(num,0,sizeof(num));
memset(tim,0,sizeof(tim));
scanf("%d%d",&n,&m);
int i,j;
int sda=0;
for(i=1;i<=n;i++){
pre[i]=i;
scanf("%d",&num[i]);
sda=max(num[i],sda);//判m=0
}
int u,v;
if(m==0){
printf("%d\n",sda);
continue;
}
for(i=1;i<=m;i++){
scanf("%d%d",&u,&v);
tim[u]++;//连的边数
tim[v]++;
int root1=find(u);
int root2=find(v);
if(root1!=root2){//并查集
pre[root1]=root2;
}
}
int flag=1;
int gg=find(v);
for(i=1;i<=n;i++){
if(find(i)!=gg){//不在一个图
if(tim[i]){//如果有别的边不在图
flag=0;
break;
}
}
}
if(!flag){
printf("Impossible\n");
continue;
}
int sss=0,ans=0;
for(i=1;i<=n;i++){
if(tim[i]==0)continue;//如果不在这个图
if(tim[i]%2)sss++;//如果连的边数是奇数次
int sa=(tim[i]+1)/2;//经过的次数
if(sa%2)ans^=num[i];
}
if(sss==2){//如果起点终点不一样
printf("%d\n",ans);
}
else if(sss==0){
int sdaa=ans;
for(i=1;i<=n;i++){
sdaa=max(sdaa,ans^num[i]);//找到异或起来最大的值
}
printf("%d\n",sdaa);
}
else printf("Impossible\n");
}
return 0;
}