the best path(欧拉回路,欧拉路径)

来源:互联网 发布:h.323端口号 编辑:程序博客网 时间:2024/05/16 04:32

2016青岛网络赛 The Best Path

The Best Path

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

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 P0P1...Pt, the lucky number of this trip would beaP0XORaP1XOR...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 (N100000) and M (M500000), as described above. The i-th line of the next Nlines contains an integer ai(i,0ai10000) 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 thatui=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
分析:
用普通数组存储每个点的价值,并用另一个普通数组记录每个点的度数。判断是欧拉回路还是欧拉路径还是都不属于。
欧拉路径:有且有两个点度数为奇数;
欧拉回路:每个点度数都为偶数。
如果是欧拉路径,直接计算点的价值的异或值。偶数个异或等于0,所以只计算奇数个的点的异或值。
如果是欧拉回路,由于每个起点多走了一次,故要找出1~n点中最后异或值最大的作为起点。
代码:
#include<iostream> 
#include<stdio.h>
#include<algorithm>
#include<string.h> 
using namespace std;
const int N=1e6;
int a[N],b[N];
int n,m,k,p,q,s,u,v;


int main()
{
int t;
scanf("%d",&t);
while(t--)
{
k=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//统计结点度数 
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
b[u-1]++;
b[v-1]++;
}
//计算度数为奇数的点的个数,欧拉回路所有点度数为偶数,欧拉路径有且有两个度数为奇数的点 
for(int i=0;i<n;i++) 
{
if(b[i]%2==1)
k++;
}
if(k==2||k==0) 
{
//先计算欧拉路径的异或值
p=0; 
for(int i=0;i<n;i++)
{
q=(b[i]+1)/2;
for(int j=0;j<q;j++)
{
p^=a[i];
}
}
if(k==2)
{
printf("%d\n",p);  
}
//如果是欧拉回路,由于端点值总是多异或一次,相当于没有异或,所以要在1~n次循环中找到异或值最大的那次
else
{
s=0;
for(int i=0;i<n;i++) 
{
if(b[i]!=0){
p=p^a[i];
s=max(s,p);
}
}
printf("%d\n",s);

}
else
printf("Impossible\n");
}
return 0;
}

0 0
原创粉丝点击