The Best Path

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

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
 
思路:判断图中存在欧拉路/欧拉回路的条件:①图连通。②图中结点的度数为奇数的个数为0/2。题意要求最大值。因为当图中存在欧拉回路时,起点要异或两次,以不同的结点为起点所得到的异或和可能不同。所以当图中存在欧拉回路时,依次遍历每个结点作为起点,求最大值即可。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = ;
struct Edge{
int u, v;
bool tag;
int getTo(int u)
{
if(this->u == u) return v;
else return this->u;
}
}es[];
int n, m, val[MAXN],deg[MAXN], res;
vector<int> arc[MAXN];
void dfs(int u)
{
for(int i = , size = arc[u].size(); i < size; i++)
{
int id = arc[u][i];
if(!es[id].tag)
{
es[id].tag = true;
int to = es[id].getTo(u);
dfs(to);
}
}
res ^= val[u];
} int par[MAXN];
void prep()
{
for(int i = ; i < MAXN; i++)
{
par[i] = i;
}
}
int fnd(int x)
{
if(x == par[x])
{
return x;
}
return par[x] = fnd(par[x]);
}
void unite(int fa, int son)
{
int a = fnd(fa);
int b = fnd(son);
par[b] = a;
}
int main()
{
// freopen("input.in", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
prep();
res = ;
memset(deg, , sizeof(deg));
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
{
arc[i].clear();
scanf("%d", &val[i]);
}
for(int i = ; i < m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
es[i].u = u;
es[i].v = v;
es[i].tag = false;
arc[u].push_back(i);
arc[v].push_back(i);
deg[u]++;
deg[v]++;
unite(u, v);
}
int start = ;
int cnt = ;
for(int i = ; i <= n; i++)
{
if(deg[i] & )
{
start = i;
cnt++;
}
}
int rt = -, sum = ;
for(int i = ; i <= n; i++)
{
int fa = fnd(i);
if(fa != rt)
{
rt = fa;
sum++;
}
}
if((cnt == || cnt == ) && sum == )
{
dfs(start);
if(cnt == )
{
printf("%d\n", res);
}
else
{
int mx = -;
for(int i = ; i <= n; i++)
{
mx = max(mx, res ^ val[i]);
}
printf("%d\n", mx);
}
}
else
{
printf("Impossible\n");
}
}
return ;
}

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

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

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

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

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

  3. hdu1161 欧拉路

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

  4. UVA10054The Necklace (打印欧拉路)

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

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

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

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

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

  7. hihocoder 1181 欧拉路.二

    传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...

  8. hiho48 : 欧拉路·一

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的 ...

  9. hdu5883 The Best Path(欧拉路)

    题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...

随机推荐

  1. bzoj1704

    题解: 贪心 枚举k 然后判断一下是否可行 代码: #include<bits/stdc++.h> using namespace std; ; int n,a[N],b[N],sum,c ...

  2. Python之Fabric

    [Fabric] Fabric是一个用Python开发的部署工具,最大特点是不用登录远程服务器,在本地运行远程命令,几行Python脚本就可以轻松部署. 安装 wget https://bootstr ...

  3. 获取手机已安装应用的name,bundleIdentitifer

    获取手机已安装应用的name,bundleIdentitifer Class c =NSClassFromString(@"LSApplicationWorkspace"); id ...

  4. maven手动添加jar(转)

    Maven 手动添加 JAR 包到本地仓库 原文链接:http://www.blogjava.net/fancydeepin/archive/2012/06/12/380605.html Maven ...

  5. Docker的安装及操作

    1. 在Ubuntu中安装Docker 更新ubuntu的apt源索引 sudo apt-get update 安装包允许apt通过HTTPS使用仓库 sudo apt-get install \ a ...

  6. PostgreSQL CPU满(100%)性能分析及优化(转)

    PostgreSQL CPU满(100%)性能分析及优化 转自:https://help.aliyun.com/knowledge_detail/43562.html    在数据库运维当中,一个DB ...

  7. HAWQ取代传统数仓实践(十九)——OLAP

    一.OLAP简介 1. 概念 OLAP是英文是On-Line Analytical Processing的缩写,意为联机分析处理.此概念最早由关系数据库之父E.F.Codd于1993年提出.OLAP允 ...

  8. [Linux] nohup/setsid/& 让进程在后台可靠运行

    当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程.因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为 ...

  9. python的if判断补充

    python的if判断补充 exit_flag = False # 标识符 if exit_flag == False: print('exit_flag == False') exit_flag = ...

  10. iOS的坑:ERRORITMS-90096: "Your binary is not optimized for iPhone 5 - New iPhone appsand app updatesXcode7提交到App Store二进制文件时报错错误:“你itms-90096二进制不是iPhone 5的新iPhone应用程序和应用程序的更新必须提交支持iPhone 5英寸的显示器........

    在工程里的Images.xcassets添加并设置LaunchImage对解决ERROR ITMS-90096根本不会起到任何作用,需要单独添加针对iPhone 5的载入图片.关键点有三项: 1.图片 ...