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 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.

 
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
题意:有 n个湖泊m条河流,现在 Alice 要从某点出发走遍所有河流一次且仅一次,问Alice走过的点的异或最大值是多少?
题解:这题每条边要走且仅走一次,那么这明显就是个欧拉(回)路,先判断一下连通分量的个数,如果有多个就是 "Impossible",然后判断度为奇数的点的个数 ,如果不是0个或者2个,输出"Impossbie" ,如果度为奇数个数为 0 个,那么就是欧拉回路,那么就从度不为0且degree/2为奇数的每个点做异或,得到一个结果,得到的结果然后分别与每个点做异或,选择最大的.如果度为奇数的点是 2 个,那么每个点走的次数便是确定的,如果是度是偶数,那么degree/2为奇数的点参与异或,如果度为奇数,那么当(degree+1)/2为奇数时,此点参与异或。
附欧拉路的定义:
判定该图是否为Euler图,包括有向欧拉通路,有向欧拉回路,无向欧拉通路,无向欧拉回路:

有向欧拉通路:起点:出度-入度=1,终点:入度-出度=1,其它点:入度==出度

有向欧拉回路:所有点:入度==出度

无向欧拉通路:仅有两个奇度点

无向欧拉回路:无奇度点

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

hdu 5833(欧拉路)的更多相关文章

  1. HDU 5883 The Best Path (欧拉路或者欧拉回路)

    题意: n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 析:由欧拉路性质,奇度点数量为0或2.一个节点被进一次出一次,度减2,产生一次贡献,因此节点 i 的贡献为 ...

  2. 欧拉路&&欧拉回路 概念及其练习

    欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...

  3. The Best Path(HDU5883)[欧拉路]2016青岛online

    题库链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 欧拉回路裸题,第一次接触欧拉路的我是真的长见识了^-^ 懂了欧拉路这道题就是没什么问题了,欧拉路 ...

  4. 洛谷P1341 无序字母对[无向图欧拉路]

    题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...

  5. POJ1386Play on Words[有向图欧拉路]

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11846   Accepted: 4050 De ...

  6. hdu1161 欧拉路

    欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...

  7. UVA10054The Necklace (打印欧拉路)

    题目链接 题意:一种由彩色珠子组成的项链.每个珠子的两半由不同的颜色组成.相邻的两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确定他们是否可以复原成完整的项链 分析:之前也没往欧拉路上面想, ...

  8. 洛谷 P1341 无序字母对 Label:欧拉路 一笔画

    题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...

  9. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

随机推荐

  1. codeforces765F Souvenirs

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  2. Hbase(六) hbase Java API

    一. 几个主要 Hbase API 类和数据模型之间的对应关系: 1. HBaseAdmin关系: org.apache.hadoop.hbase.client.HBaseAdmin作用:提供了一个接 ...

  3. bzoj1969: [Ahoi2005]LANE 航线规划(树链剖分)

    只有删边,想到时间倒流. 倒着加边,因为保证图连通,所以一开始一定至少是一棵树.我们先建一棵树出来,对于每一条非树边,两个端点在树上这段路径的边就不会变成关键边了,所以将它们对答案的贡献删去,那么直接 ...

  4. 4.UiCollection API 详细介绍

    一.UiCollection类介绍 UiCollection类两大功能:从集合中查找对象:获取某种搜索条件组件的数量 1.UiCollection类说明 1)UiCollection是UiObject ...

  5. CF821 A. Okabe and Future Gadget Laboratory 水

    Link 题意:询问n X n中非1数是否能够由同行同列中分别取两个数做和得到. 思路:水题. /** @Date : 2017-07-03 16:23:18 * @FileName: A.cpp * ...

  6. 一般处理程序、ASP.NET核心知识(5)

    初窥 1.新建一个一般处理程序 新建一个一般处理程序 2.看看里头的代码 public class MyHandler : IHttpHandler { public void ProcessRequ ...

  7. 【总结】对FFT的理解 / 【洛谷 P3803】 【模板】多项式乘法(FFT)

    题目链接 \(\Huge\text{无图,慎入}\) \(FFT\)即快速傅里叶变换,用于加速多项式乘法. 如果暴力做卷积的话就是一个多项式的每个单项式去乘另一个多项式然后加起来,时间复杂度为\(O( ...

  8. React Native 与 夜神模拟器的绑定

    之前一直用真机去调试, 每回更新一次都需要手动摇晃手机后才能reload JS, OMG,太麻烦了. 后来寻思模拟器网上推荐用Geny...什么的模拟器,但是那个模拟器还需要VBox一起用. 有点麻烦 ...

  9. struts入门

    1.概念

  10. (2)剑指Offer之二维数组查找和替换空格问题

    一 二维数组查找 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 问 ...