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. Prism 4 文档 ---第4章 模块化应用程序开发

    模块化应用程序是指将一个应用程序拆分成一系列的可以组合的功能单元.一个客户端模块封装了应用程序的一部分,并且通常是一系列相关的关注点.它可以包含一个相关的组件的集合,就像用户界面,应用程序功能,和一些 ...

  2. hdu3488

    题解: 首先把每一个点拆到两边 然后做KM求最大 吧没一条边相反即可 代码: #include<cstdio> #include<cmath> #include<algo ...

  3. New Concept English there (5)

    25w/m Editors of newspapers and magazines often go to extremes to provide their readers with unimpor ...

  4. JSP和JS的区别

    从本科毕业设计开始就一直困扰我,jsp和js这两者的区别,一直处于迷糊状态,也没有搞清楚.今天就简单的介绍下两者的区别. 1.JSP全称是java server page    JS全称是javaSc ...

  5. Shell 命令行实现将一个站点页面全部下载到本地并替换其中链接的脚本

    Shell 命令行实现将一个站点页面全部下载到本地并替换其中链接的脚本 不知道为什么,我总想用 Shell 脚本来实现把一个站点内容给下载下来.但是下载什么站点我确不知道.今天尝试了一下利用 curl ...

  6. UE4中类自动生成代码解析

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/73189272 作者:car ...

  7. Leetcode 590. N-ary Tree Postorder Traversal

    DFS,递归或者栈实现. """ # Definition for a Node. class Node: def __init__(self, val, childre ...

  8. word-break:break-all 打散文字,强制对齐

  9. runtime 知识点

    demo https://github.com/ZOYOOPlus/runtime2 // //  ViewController.m //  runtime //  Copyright © 2017年 ...

  10. Aria2 - OS X 下载百度云资源神器

    官网介绍: (Aria2 is a light weight multi-protocol & multi-source command-line download utility. It s ...