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. 转载:【Oracle 集群】RAC知识图文详细教程(五)--特殊问题和实战经验

    文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...

  2. C#笔记 -- 协变、逆变

    协变 理解:在泛型和委托中, 让使用某个泛型参数A的类型可以用一个使用由A派生的泛型参数B的类型实例化,(小=> 大)如 ​ // IEnumerable<Animal> 与 Lis ...

  3. 12.18 分布式系统下的session

    广义的session: 会话控制,可以理解成为一种保存key-value的机制 从key的方面来看:sessionId和token sessionId: 服务端请求客户端的时候,服务端通过setcoo ...

  4. sed用法详解

    转载自: SED单行脚本快速参考(Unix 流编辑器) 如侵犯您的版权,请联系:Windeal12@qq.com ------------------------------------------- ...

  5. c# 验证码图片生成类

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...

  6. Android Hook框架Xposed详解

    1 Introduction 1.1  概述 Xposed 是 GitHUB 上 rovo89 大大设计的一个针对 Android 平台的动态劫持项目,通过替换 /system/bin/app_pro ...

  7. a链接嵌套无效,嵌套链接最优解决办法

    <a>不支持嵌套.例如: <a href="#1">11111111111<a href="#2">22222222222& ...

  8. Java中有两种实现多线程的方式以及两种方式之间的区别

    看到一个面试题.问两种实现多线程的方法.没事去网上找了找答案. 网上流传很广的是一个网上售票系统讲解.转发过来.已经不知道原文到底是出自哪里了. Java中有两种实现多线程的方式.一是直接继承Thre ...

  9. 字符串处理scanf("%d%*c",&n);

    "*"表示该输入项读入后不赋予任何变量,即跳过该输入值.这在减小内存开支上面还是有一点用处的,不需要的字符直接跳过,免得申请没用的变量空间 你的例子中的%*c的作用是读入'\n', ...

  10. Linux下Eclipse配置安装 PyDev(Pydev插件一直不能成功,安装这个插件失败的问题)

    pydev插件安装方式如果采取从网络上下载,然后解压到eclipse中文件夹到方式,运行到时候可能会导致重启eclipse后根本看不到这个插件! 原因以及解决方式,看下面!  转自:http://ww ...