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 P0→P1→...→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 (N≤100000) and M (M≤500000), as described above. The i-th line of the next Nlines 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 thatui=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 Nlines 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 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;
}
#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
- the best path(欧拉回路,欧拉路径)
- hdu 5883 The Best Path 欧拉路径(回路)
- hdu 5883 The Best Path 欧拉回路(路径)
- HDU 5883 The Best Path(欧拉路径\回路)
- HDU 5883 The Best Path 欧拉路径/回路
- hdu5883 The Best Path(欧拉通路、欧拉回路)
- HDU5883 The Best Path(欧拉回路)
- hdu 5883 The Best Path 欧拉路径 & 欧拉回路 并查集
- hdu5883The Best Path(欧拉通路&&欧拉回路)
- HDU-5883 The Best Path(欧拉通路和欧拉回路)
- HDU 5880 The Best Path 欧拉回路+异或 *
- HDU 5883 The Best Path(欧拉道路+回路)
- The Best Path 并查集 + 欧拉回路
- hdoj5883The Best Path【欧拉回路】
- hdu 5883 The Best Path【欧拉路经/欧拉回路】
- HDU 5883 The Best Path(欧拉通路与欧拉回路)
- 欧拉回路(欧拉路径)
- 欧拉回路(欧拉路径)
- JavaScript基础知识应用案例分享---滑轮滚动浏览页面
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- wordpress加入站长统计功能
- 【数据库技术与应用】课程小结1.1+1.2
- 关于父类子类继承关系
- the best path(欧拉回路,欧拉路径)
- 快速排序
- 关于父类子类继承关系
- Python安装
- Java EE - Spring MVC 数据绑定实例
- poj1094 Sorting it all out(拓扑排序)
- 160923基础小结
- 07:可变长数组、多维数组
- NSDate分类-日期格式化以及日期常用判断