大体题意:
给你一个无向图,每个点有权值,你要从某一个点出发,使得一笔画经过所有的路,且使得经过的节点的权值XOR运算最大!求最大值!
思路:
首先得知道一些关于XOR运算的性质:
1.如果一个数异或偶数次,那么这个结果是0!
2.如果一个序列是确定的,那么异或的顺序不影响最终的结果!
有了这两个性质,那么这个题就很简单了!
首先它得是一条欧拉通路:
1.它必须是联通的,对于这个题目,如果发现的边有很多联通快,这肯定是不合法的!
2.其次,计算每个点的度数,如果奇点的个数不为0 并且不为2,那么也是不合法!
3.然后只要知道每个点的度数就可以算了,根本不用dfs 去模拟路径!
在纸上画一画就知道,当这个点的(度数+1)/2 是奇数的话,要进行异或,否则相当于没有异或(变成0)
注意:
这个题之所以问最大值,是因为有可能有欧拉回路!欧拉回路是任意选起点的,起点要经过两次,这样就抵消了,会产生一个最大值!
枚举一遍即可!
详细见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn],fa[maxn],d[maxn];
int find(int x){
return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
void add(int x,int y){
int xx = find(x), yy = find(y);
if (xx != yy)fa[xx] = yy;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d %d",&n, &m);
for (int i = 0; i <= n; ++i)fa[i] = i;
memset(d,0,sizeof d);
for (int i = 1; i <= n; ++i)scanf("%d",&a[i]);
for (int i = 0; i < m; ++i){
int u,v;
scanf("%d %d",&u, &v);
add(u,v);
d[u]++, d[v]++;
}
int odd = 0;
bool ok = 1;
int cnt = 1;
for (int i = 1; i <= n; ++i){
if (d[i]){
cnt = i;
break;
}
}
for (int i = 1; i <= n; ++i){
odd += (d[i] & 1);
}
if (odd != 0 && odd != 2)ok = 0;
for (int i = 1; i <= n; ++i){
if (!d[i])continue;
if (find(i) != find(cnt)){
ok=0;
break;
}
}
if (!ok){
printf("Impossible\n");
continue;
}
int ans = 0;
for (int i = 1; i <= n; ++i){
if (((d[i]+1)/2) & 1)ans ^= a[i];
}
//printf("odd = %d\n",ans);
if (odd == 0){
int tmp = ans;
for (int i = 1; i <= n; ++i){
ans = max(ans,tmp^a[i]);
}
}
printf("%d\n",ans);
}
return 0;
}
/**
3 3
3 4 5
1 2
2 3
3 1
**/
The Best Path
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 447 Accepted Submission(s): 186
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
Recommend