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 .
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
Source
一个无向图,n个点,m条边,要求将所有边都遍历且仅遍历一次。n个点有其点权,每一次到达这个点将这个值进行异或,求最大的异或值。
由于异或运算是满足交换律的,所以其实只需要计算这个点的权进行运算的次数就可以,满足这个值最大。
首先,这个图必须满足欧拉通路或欧拉回路,否则没办法将所有边都只遍历一次。也就是计算他们的度就可以。然后通路和回路分别讨论。
常规处理:因为经过一个点需要消耗该点的2个度,一个点的度个数为degree,那么路径需要经过该点degree/2次,如果(degree/2)%2==1,则直接异或上此值;如果(degree/2)%2==0,由异或的性质偶数个相同的数异或后为0,则不用计算该点;
如果是回路,先遍历常规处理所有点求出异或值,之后遍历选择一个点作为起点和终点,找寻选择哪个点可以取得最大值。
如果是通路,此时会有2个奇度顶点,遍历所有点,如果某个点是奇度顶点,异或并度数减1,然后如上常规处理;偶度顶点常规处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int MAXN=100010;
vector<int> g[MAXN];
bool visited[MAXN];
int w[MAXN];
int degree[MAXN];
int n,m;
void dfs(int cur)
{
visited[cur]=1;
for(int i=0;i<g[cur].size();i++)
if(!visited[g[cur][i]])
dfs(g[cur][i]);
}
int isloop()
{
int num=0;
for(int i=1;i<=n;i++)
if(degree[i]%2==1)
num++;
if(num==2)
return 2;
else if(num==0)
return 1;
else
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=0;i<=MAXN;i++)
g[i].clear();
memset(visited,0,sizeof(visited));
memset(degree,0,sizeof(degree));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
degree[a]++;
degree[b]++;
}
int cnt=0;
for(int i=1;i<=n;i++)
if(!visited[i])
{
cnt++;
if(cnt>1)
break;
dfs(i);
}
int flag=isloop();
if(cnt>1||flag==0)
{
cout<<"Impossible"<<endl;
continue;
}
int ans=0;
if(flag==1)//欧拉回路
{
for(int i=1;i<=n;i++)
if((degree[i]/2)%2==1)
ans^=w[i];
ans^=w[1];
for(int i=2;i<=n;i++)
ans=max(ans,ans^w[i]);
}
if(flag==2)//欧拉通路
{
for(int i=1;i<=n;i++)
{
if(degree[i]%2==1)
{
ans^=w[i];
degree[i]--;
}
if((degree[i]/2)%2==1)
ans^=w[i];
}
}
cout<<ans<<endl;
}
return 0;
}