HDU 5883 The Best Path (枚举 欧拉通路)
来源:互联网 发布:js this 改变指向 编辑:程序博客网 时间:2024/05/15 12:05
大体题意:
给你一个无向图,每个点有权值,你要从某一个点出发,使得一笔画经过所有的路,且使得经过的节点的权值XOR运算最大!求最大值!
思路:
首先得知道一些关于XOR运算的性质:
1.如果一个数异或偶数次,那么这个结果是0!
2.如果一个序列是确定的,那么异或的顺序不影响最终的结果!
有了这两个性质,那么这个题就很简单了!
首先它得是一条欧拉通路:
1.它必须是联通的,对于这个题目,如果发现的边有很多联通快,这肯定是不合法的!
2.其次,计算每个点的度数,如果奇点的个数不为0 并且不为2,那么也是不合法!
3.然后只要知道每个点的度数就可以算了,根本不用dfs 去模拟路径!
在纸上画一画就知道,当这个点的(度数+1)/2 是奇数的话,要进行异或,否则相当于没有异或(变成0)
注意:
这个题之所以问最大值,是因为有可能有欧拉回路!欧拉回路是任意选起点的,起点要经过两次,这样就抵消了,会产生一个最大值!
枚举一遍即可!
详细见代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100000 + 10;int a[maxn],fa[maxn],d[maxn];int find(int x){ return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);}void add(int x,int y){ int xx = find(x), yy = find(y); if (xx != yy)fa[xx] = yy;}int main(){ int T; scanf("%d",&T); while(T--){ int n,m; scanf("%d %d",&n, &m); for (int i = 0; i <= n; ++i)fa[i] = i; memset(d,0,sizeof d); for (int i = 1; i <= n; ++i)scanf("%d",&a[i]); for (int i = 0; i < m; ++i){ int u,v; scanf("%d %d",&u, &v); add(u,v); d[u]++, d[v]++; } int odd = 0; bool ok = 1; int cnt = 1; for (int i = 1; i <= n; ++i){ if (d[i]){ cnt = i; break; } } for (int i = 1; i <= n; ++i){ odd += (d[i] & 1); } if (odd != 0 && odd != 2)ok = 0; for (int i = 1; i <= n; ++i){ if (!d[i])continue; if (find(i) != find(cnt)){ ok=0; break; } } if (!ok){ printf("Impossible\n"); continue; } int ans = 0; for (int i = 1; i <= n; ++i){ if (((d[i]+1)/2) & 1)ans ^= a[i]; } //printf("odd = %d\n",ans); if (odd == 0){ int tmp = ans; for (int i = 1; i <= n; ++i){ ans = max(ans,tmp^a[i]); } } printf("%d\n",ans); } return 0;}/**3 33 4 51 22 33 1**/
The Best Path
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 447 Accepted Submission(s): 186
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 integersN (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.
Thei -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 .
For each test case, in the first line there are two positive integers
The
Output
For each test cases, output the largest lucky number. If it dose not have any path, output "Impossible".
Sample Input
23 23451 22 34 312341 22 32 4
Sample Output
2Impossible
Source
2016 ACM/ICPC Asia Regional Qingdao Online
Recommend
wange2014 | We have carefully selected several similar problems for you: 5901 5900 5899 5898 5897
0 0
- HDU 5883 The Best Path (枚举 欧拉通路)
- HDU-5883 The Best Path(欧拉通路和欧拉回路)
- HDU 5883 The Best Path (一笔画 / 欧拉通路)
- HDU 5883 The Best Path(欧拉通路与欧拉回路)
- HDU 5883 The Best Path (欧拉通路) 2016 ACM/ICPC Asia Regional Qingdao Online
- hdu5883 The Best Path(欧拉通路、欧拉回路)
- hdu 5883 The Best Path 欧拉路径(回路)
- hdu 5883 The Best Path 欧拉回路(路径)
- hdu5883The Best Path(欧拉通路&&欧拉回路)
- HDU 5883 The Best Path(欧拉路径\回路)
- HDU 5883 The Best Path(欧拉道路+回路)
- HDU 5883 The Best Path 欧拉路径/回路
- hdu 5883 The Best Path【欧拉路经/欧拉回路】
- hdu 5883 The Best Path 欧拉路径 & 欧拉回路 并查集
- HDU 5880 The Best Path 欧拉回路+异或 *
- the best path(欧拉回路,欧拉路径)
- hdu 5883 The Best Path
- HDU 5883 - The Best Path
- 【笔试】店铺选址+最短路径
- Java自动装箱与拆箱
- 漫话中文自动分词和语义识别(上):中文分词算法
- 实现苹果系统自带的侧滑返回上一级效果
- Matlab 中的线性规划函数使用方法
- HDU 5883 The Best Path (枚举 欧拉通路)
- JS制作字体图
- 大话数据库:第四章 创建和维护表
- 写一篇文章,几下以前看过的动漫和电视吧
- QRunnable如何返回执行信息
- 快速编译出WebRTC for Android 的一次记录
- 轮播图的javascript写法
- Game 框架
- Mybatis接口编程原理分析(三)