HDOJ5883(欧拉路)
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
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.
4
5
#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(欧拉路)的更多相关文章
- 洛谷P1341 无序字母对[无向图欧拉路]
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- hdu1161 欧拉路
欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...
- UVA10054The Necklace (打印欧拉路)
题目链接 题意:一种由彩色珠子组成的项链.每个珠子的两半由不同的颜色组成.相邻的两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确定他们是否可以复原成完整的项链 分析:之前也没往欧拉路上面想, ...
- 洛谷 P1341 无序字母对 Label:欧拉路 一笔画
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- hihocoder 1181 欧拉路.二
传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...
- hiho48 : 欧拉路·一
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的 ...
- hdu5883 The Best Path(欧拉路)
题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...
随机推荐
- bzoj1704
题解: 贪心 枚举k 然后判断一下是否可行 代码: #include<bits/stdc++.h> using namespace std; ; int n,a[N],b[N],sum,c ...
- Python之Fabric
[Fabric] Fabric是一个用Python开发的部署工具,最大特点是不用登录远程服务器,在本地运行远程命令,几行Python脚本就可以轻松部署. 安装 wget https://bootstr ...
- 获取手机已安装应用的name,bundleIdentitifer
获取手机已安装应用的name,bundleIdentitifer Class c =NSClassFromString(@"LSApplicationWorkspace"); id ...
- maven手动添加jar(转)
Maven 手动添加 JAR 包到本地仓库 原文链接:http://www.blogjava.net/fancydeepin/archive/2012/06/12/380605.html Maven ...
- Docker的安装及操作
1. 在Ubuntu中安装Docker 更新ubuntu的apt源索引 sudo apt-get update 安装包允许apt通过HTTPS使用仓库 sudo apt-get install \ a ...
- PostgreSQL CPU满(100%)性能分析及优化(转)
PostgreSQL CPU满(100%)性能分析及优化 转自:https://help.aliyun.com/knowledge_detail/43562.html 在数据库运维当中,一个DB ...
- HAWQ取代传统数仓实践(十九)——OLAP
一.OLAP简介 1. 概念 OLAP是英文是On-Line Analytical Processing的缩写,意为联机分析处理.此概念最早由关系数据库之父E.F.Codd于1993年提出.OLAP允 ...
- [Linux] nohup/setsid/& 让进程在后台可靠运行
当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程.因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运行在新的会话里从而成为 ...
- python的if判断补充
python的if判断补充 exit_flag = False # 标识符 if exit_flag == False: print('exit_flag == False') exit_flag = ...
- iOS的坑:ERRORITMS-90096: "Your binary is not optimized for iPhone 5 - New iPhone appsand app updatesXcode7提交到App Store二进制文件时报错错误:“你itms-90096二进制不是iPhone 5的新iPhone应用程序和应用程序的更新必须提交支持iPhone 5英寸的显示器........
在工程里的Images.xcassets添加并设置LaunchImage对解决ERROR ITMS-90096根本不会起到任何作用,需要单独添加针对iPhone 5的载入图片.关键点有三项: 1.图片 ...