Files
python/TangDou/AcWing/EulerPath/HDU5883.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

59 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 7;
int n, m;
int a[N];
int d[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("HDU5883.in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--) {
memset(d, 0, sizeof d);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
d[a]++, d[b]++;
}
// 奇数度点的个数
int cnt = 0;
for (int i = 1; i <= n; i++)
if (d[i] & 1) cnt++;
// 不是0,而且个数不是2,那么肯定没有欧拉通路,也没有欧拉回路
if (cnt && cnt != 2) {
puts("Impossible");
continue;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int x = (d[i] + 1) >> 1;
if (x & 1) ans ^= a[i];
}
// 如果没有度数为奇数的点,也就是欧拉回路,出发点终点是同一个点,这样会造成起点的权值被异或干掉
// 因为欧拉回路可以以任意一个点做为起点和终点,所以,需要枚举一遍,分别尝试以i这起点,找出最大异或值
int res = ans;
if (cnt == 0)
for (int i = 1; i <= n; i++) res = max(res, ans ^ a[i]);
// 输出异或和
printf("%d\n", res);
}
return 0;
}