# The Best Path

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 18    Accepted Submission(s): 8

Problem Description
Alice is planning her travel route in a beautiful valley. In this valley, there are

Input
The first line of input contains an integer

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 100005;
int a[N];
int father[N];
int degree[N];
int _find(int x)
{
if(x!=father[x]) father[x] = _find(father[x]);
return father[x];
}
void Union(int a,int b)
{
int x = _find(a);
int y = _find(b);
if(x==y) return ;
father[x] = y;
}
void init(int n)
{
for(int i=1; i<=n; i++)
{
father[i] = i;
degree[i] = 0;
}
}
int main()
{
int tcase,n,m;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d",&n,&m);
init(n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int i=1; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
degree[u]++;
degree[v]++;
Union(u,v);
}
int ans = 0;
for(int i=1; i<=n; i++)
{
if(degree[i]>0)
{
if(_find(i)==i) ans++;
}
}
///连通分量个数
if(ans>1)
{
printf("Impossible\n");
continue;
}
ans = 0;
for(int i=1; i<=n; i++)
{
if(degree[i]%2!=0) ans++;
}
///判断是否为欧拉路
if(ans!=0&&ans!=2)
{
printf("Impossible\n");
continue;
}
LL res = 0;
if(ans==0)
{
for(int i=1; i<=n; i++)
{
int K = degree[i]/2;
if(degree[i]!=0&&K%2==1){
res = res^a[i];
}
}
LL MAX = -1;
for(int i=1; i<=n; i++)
{
int K = degree[i]/2;
if(degree[i]!=0){
MAX = max(MAX,res^a[i]);
}
}
res = MAX;
}
else
{
for(int i=1; i<=n; i++)
{
int K = degree[i]/2;
if(degree[i]%2==0&&K%2==1)
{
res = res^a[i];
}
if(degree[i]%2==1)
{
K = (degree[i]+1)/2;
if(K%2==1)
res = res^a[i];
}
}
}
printf("%lld\n",res);
}
return 0;
}

posted @ 2016-09-17 18:16  樱花庄的龙之介大人  阅读(...)  评论(...编辑  收藏