【刷题】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个数,其中只有一个数出现了一次,其他都是两次,找出那 ...
随机推荐
- jquery选择器练习
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- ME_PROCESS_PO_CUST 实现采购订单行项目增强
用户希望创建采购订单时,输入行项目时,能根据采购订单类型,自动带出科目分类类别. 业务顾问看了一下配置,不能实现这个功能,所以用增强实现. 采购订单BADI增强:ME_PROCESS_PO_CUST. ...
- 大数据入门第二十五天——elasticsearch入门
一.概述 推荐路神的ES权威指南翻译:https://es.xiaoleilu.com/010_Intro/00_README.html 官网:https://www.elastic.co/cn/pr ...
- WPF 初学VisifireChart
visifire今天登陆他们官网的时候,发现好像是挂掉了,不知道是不再运营了,还是单纯服务器出了问题. VisifireChart的效果不炫,但是对于一些项目,感觉够用的,所以,今天大概看了几篇博客, ...
- python基础学习1-流程控制和判断
python for循环和 if流程控制用法 Ages=22 for i in range(10): inputAges = int(input("输入年龄")) if input ...
- CF891C Envy
题面 题解 首先要知道两个性质: 对于任意权值,最小生成树上该权值的边数是相同的. 对于任意一个最小生成树,当加完所有权值小于一个任意值的边之后,当前图的连通性是一样的. 于是我们按照权值分开处理,对 ...
- LOJ #2135. 「ZJOI2015」幻想乡战略游戏
#2135. 「ZJOI2015」幻想乡战略游戏 链接 分析: 动态点分治,求加权重心,带修改. 考虑如果知道了一个点s,如何求答案,那么首先可以点分治的思想,求每个联通块内所有点到分治中心距离和,然 ...
- [HNOI2018]转盘[结论+线段树]
题意 题目链接 分析 首先要发现一个结论:最优决策一定存在一种 先在出发点停留之后走一圈 的情况,可以考虑如下证明: 如果要停留的话一定在出发点停留,这样后面的位置更容易取到. 走超过两圈的情况都可以 ...
- 基于.NET Standard的分布式自增ID算法--美团点评LeafSegment
概述 前一篇文章讲述了最流行的分布式ID生成算法snowflake,本篇文章根据美团点评分布式ID生成系统文章,介绍另一种相对更容易理解和编写的分布式ID生成方式. 实现原理 Leaf这个名字是来自德 ...
- 获取【酷我音乐】歌曲URL地址
非原创 酷我中的歌曲的页面地址通常是:www.kuwo.cn/yinyue/6181801,末尾的一串数字是这首歌曲的编号id. 我们只须在 http://player.kuwo.cn/webmusi ...