【刷题】HDU 5883 The Best Path
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 \(P_0 \rightarrow P_1 \rightarrow ... \rightarrow P_t\) , the lucky number of this trip would be \(a_{P_0} \quad XOR \quad a_{P_1} \quad XOR \quad... \quad XOR \quad a_{P_t}\) . 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
Description(CHN)
给你一个无向图,每个点有权值,你要从某一个点出发,使得一笔画经过所有的路,且使得经过的节点的权值XOR运算最大
Solution
对于一个图,如果奇度数点数不为0也不为2,那么无解
如果为2,则欧拉路径固定,每个点经过的次数为它的度数/2,直接算就可以了
如果为0,即存在欧拉回路,这种情况相较于上一种情况就是起点多经过了一次,因为要回到起点,所以枚举起点,chkmax就好了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100000+10;
int T,n,m,val[MAXN],d[MAXN],sum[2],ans,now;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
read(T);
while(T--)
{
read(n);read(m);
for(register int i=1;i<=n;++i)read(val[i]),d[i]=0;
for(register int i=1;i<=m;++i)
{
int u,v;read(u);read(v);
d[u]++;d[v]++;
}
sum[0]=sum[1]=0;
for(register int i=1;i<=n;++i)sum[d[i]&1]++;
if(sum[1]!=0&&sum[1]!=2)
{
puts("Impossible");
continue;
}
ans=now=0;
for(register int i=1;i<=n;++i)
if((d[i]>>1)&1)now^=val[i];
if(sum[1]==2)
{
for(register int i=1;i<=n;++i)
if(d[i]&1)now^=val[i];
ans=now;
}
else
for(register int i=1;i<=n;++i)chkmax(ans,now^val[i]);
write(ans,'\n');
}
return 0;
}
【刷题】HDU 5883 The Best Path的更多相关文章
- HDU 5883 The Best Path
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 5883 The Best Path (欧拉路或者欧拉回路)
题意: n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 析:由欧拉路性质,奇度点数量为0或2.一个节点被进一次出一次,度减2,产生一次贡献,因此节点 i 的贡献为 ...
- 【刷题-PAT】A1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)
前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...
- 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)
转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...
- 教你用python写:HDU刷题神器
声明:本文以学习为目的,请不要影响他人正常判题 HDU刷题神器,早已被前辈们做出来了,不过没有见过用python写的.大一的时候见识了学长写这个,当时还是一脸懵逼,只知道这玩意儿好屌-.时隔一年,决定 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- 【刷题】HDU 2222 Keywords Search
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- leetcode刷题总结一
大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维 Single Number: 有N个数,其中只有一个数出现了一次,其他都是两次,找出那 ...
随机推荐
- vmware共享文件夹
环境: VMware Workstation 11.0 虚拟机中的系统:Ubuntu 16.04 物理机:window 7 安装好vmware tools后在 /mnt/hgfs 里没有东西,是空白的 ...
- Linux中的do{...} while(0)
摘抄自: <<Linux设备驱动开发详解(第二版)>> 在 Linux 内核中,经常会看到do{} while(0)这样的语句,许多人开始都会疑惑,认为do{} while(0 ...
- Mybatis初步详细配置
1.Mybatis所需包 下载地址:https://github.com/mybatis/mybatis-3/releases,其中log4j是日志包,mysql是数据库所需包,需自行下载 2.项目结 ...
- POJ3278&&1426&&3126&&3087&&3414
接上次的,标签是BFS专题 其实无论是Deepth还是Breadth都是Search 3278 又是撸过的题目 1426 找一个十进制数(我刚开始看样例以为是二进制数),使得它每一位上都是1或0,且是 ...
- P问题,NP问题,NPC问题,NP-hard问题
1.P问题:一个问题能找到一个在多项式时间里解决他的算法 多项式时间(o(1),o(lgn),o(n的a次方)) 非多项式时间 o(a的n次方) o(n!) 2.NP问题:在多项式时间找不到问题的解 ...
- 洛咕 P4491 [HAOI2018]染色
显然颜色数量不会超过\(lim=\min(m,n/S)\) 考虑容斥,计算恰好出现了\(S\)次的颜色有至少\(i\)种的方案数\(f[i]\),钦定\(i\)种颜色正好放\(S\)种 有\(m\)种 ...
- 手撸orm框架
一 前言 1 我在实例化一个user对象的时候,可以user=User(name='lqz',password='123') 2 也可以 user=User() user['name']='lqz' ...
- Markdown打造高逼格博客
这里首先假设读者你已经掌握了Markdown与GitHub的基本用法 如果不会, 请先自行百度或Google, 我目前还没写Markdown与GitHub的教程 看云只是一个推荐, 可以认为协助生成格 ...
- install opencv 2.4.10 with issue :"nvcc fatal : Unsupported gpu architecture 'compute_11'"
issue: nvcc fatal : Unsupported gpu architecture 'compute_11'CMake Error at cuda_compile_generated ...
- CSS技巧收集——毛玻璃效果
先上 demo和 源码 其实毛玻璃的模糊效果技术上比较简单,只是用到了 css 滤镜(filter)中的 blur 属性.但是要做一个好的毛玻璃效果,需要注意很多细节. 比如我们需要将上图中页面中间的 ...