俗称“一笔画”,这题就考欧拉路径的基础概念。
题意:给定一个无向图,关于欧拉路的问题。这里我们要求的是这个路径的结点异或在一起的最大值。
先判断是否构成欧拉路径。
再根据异或的偶数次相消,奇数次原数的规律,找每个结点(入度+出度)度为奇数次的,异或一下。
如果是欧拉回路,起点与终点未知但相同,所以起点要多异或一次。在上面的基础上,遍历异或取最大即可。
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;
const int mod=1e9+7;
int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*f;
}
int T,n,m,a[maxn],d[maxn];
int main(){
T=read();
while(T--){
memset(d,0,sizeof(d));
n=read(); m=read();
for(int i=1;i<=n;i++) a[i]=read();
while(m--){
int x,y;
x=read(); y=read();
d[x]++; d[y]++;
}
//判断是否存在欧拉通/回路
int cnt=0;
for(int i=1;i<=n;i++){
if(d[i]&1) cnt++;
}
if(cnt&&cnt!=2){
printf("Impossible\n");
continue;
}
int ans=0,temp=0;
for(int i=1;i<=n;i++) {
d[i]=(d[i]+1)>>1; //向上取整
if(d[i]&1) ans^=a[i];
}
//欧拉回路时,起点与终点未知且相等,要多取一次起点
if(!cnt){
for(int i=1;i<=n;i++) temp=max(temp,ans^a[i]);
ans=temp;
}
printf("%d\n",ans);
}
}
/*
1
6 10
1 2 4 8 16 32
1 6
1 5
1 4
1 2
2 5
2 4
2 3
3 4
4 5
5 6
52
*/