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. Markdown - 让网络书写变得简单

    概述 宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所 ...

  2. CF911A

    题解: 先按照a大小排序(要双关键字) 然后和a[1]一样的按照b减一减,取最小 代码: #include<bits/stdc++.h> using namespace std; ; in ...

  3. Spring Framework Artifacts

    GroupId ArtifactId Description org.springframework spring-aop Proxy-based AOP support org.springfram ...

  4. Spring整合Hibernate:1、annotation方式管理SessionFactory

    1.在applicationContext.xml文件中初始化SessionFactory(annotation方式) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...

  5. (转)Android学习笔记②——HelloWorld的创建已经基本知识

    开发第一应用 可以开发属于自己的应用,是否有点小激动?好吧!让我们开始,首先点击Start a new Android Studio Project创建工程:接下来需要输入应用名称(第一个字母要大写) ...

  6. Chrome自定义最小字号

    ============= ============== =======================

  7. Android SDK无法更新解决办法

    一.设置SDK代理 启动Android SDK Manager,选择菜单 Tools ->Options -> 代理地址: mirrors.opencas.cn 代理端口: 80 如下图: ...

  8. Eclipse快捷键详细解析

    android开发中常用的Eclipse快捷键详细解析 1.查看快捷键定义的地方 Window->Preferences->General->Keys. 2.更改启动页 在Andro ...

  9. Linux内核源代码目录结构详解

    http://blog.csdn.net/u013014440/article/details/44024207

  10. 前端神器!!gulp livereload实现浏览器自动刷新

    首先gulp是基于Node的,所以确保你已经安装 node.js,在Nodejs官方网站下载跟自己操作系统相对应的安装包. 先说一下gulp安装流程: 1:全局安装gulp,操作为: npm inst ...