题意:n个点m条无向边的图,找一个欧拉通路/回路,下标是p1,p2,p3…pt,然后使得ap1XORap2XOR…XORapt这个值最大。

思路:

首先要判断一下这个图是不是联通的,用并查集就好了,然后有个注意点就是可能是单个独立点;

然后再判断是不是欧拉通路,不是也不行;

最后计算,最后如果是欧拉回路还要找一个最大起点(终点)。

#include <bits/stdc++.h>
using namespace std; const int N=1e5+10;
int n; int pre[N];
int a[N];
int in[N]; int Find(int x)
{
int r=x;
while(pre[r]!=r)
r=pre[r];
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
} void Union(int x,int y)
{
int xx=Find(x);
int yy=Find(y);
if(xx!=yy)
pre[xx]=yy;
} void init()
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pre[i]=i;
in[i]=0;
}
} int main()
{
int T,m,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
in[x]++;
in[y]++;
Union(x,y);
}
int flag=0;
for(int i=1;i<=n;i++)
{
if(pre[i]==i&&in[i])
flag++;
if(flag==2)
break;
}
if(flag==2)
{
puts("Impossible");
continue;
}
int res,num=0;
for(int i=1;i<=n;i++)
{
if(in[i]&1)
num++;
}
if(!(!num||num==2))
{
puts("Impossible");
continue;
}
int ans=0;
for(int i=1;i<=n;i++)
{
res=in[i];
if(res%2)
{
res/=2;
if((res+1)%2)
ans^=a[i];
}
else
{
res/=2;
if(res%2)
ans^=a[i];
}
}
//printf("%d\n",ans);
if(!num)
{
res=ans;
for(int i=1;i<=n;i++)
{
res=max(res,ans^a[i]);
}
printf("%d\n",res);
}
else
printf("%d\n",ans);
}
return 0;
}

hdu5883【欧拉通路】的更多相关文章

  1. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

  2. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  3. POJ 1780 Code(有向图的欧拉通路)

    输入n(1<=n<=6),输出长度为10^n + n -1 的字符串答案. 其中,字符串以每n个为一组,使得所有组都互不相同,且输出的字符串要求字典序最小. 显然a[01...(n-1)] ...

  4. HDU1116 Play on Words(有向图欧拉通路)

    我把单词当作点,然后这样其实是不对的,这样就要判定是否是哈密顿通路.. 这题应该把单词的首尾单词当作点,而单词本身就是边,那样就是判定欧拉通路了. 有向图包含欧拉通路的充要条件是:首先基图连通,然后是 ...

  5. 欧拉通路-Play on Words 分类: POJ 图论 2015-08-06 19:13 4人阅读 评论(0) 收藏

    Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10620 Accepted: 3602 Descri ...

  6. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  7. POJ 1386 有向图欧拉通路

    题意:给你一些字符串,这些字符串可以首位相接(末位置如果和另一个字符串的首位置相同的话就可以相连) .然后问你是否可以全部连起来. 思路:就是取出每个字符串的首尾位置,然后求出出度和入度,根据有向欧拉 ...

  8. hdu 1116 Play on Words(欧拉通路)

    Problem Description Some of the secret doors contain a very interesting word puzzle. The team of arc ...

  9. CodeForces - 508D Tanya and Password(欧拉通路)

    Description While dad was at work, a little girl Tanya decided to play with dad characters. She has ...

随机推荐

  1. [haoi2011]a

    一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) 题解:首先,由每个人说的话的内容,我们可以理解为他处在ai+1,n-bi ...

  2. 10.19-10.20 test

    2016 10.19-10.20 两天  题目by mzx Day1: T1:loverfinding 题解:hash #include<iostream> #include<cst ...

  3. ES6 新特性之Symbol

    Symbol let s1 = Symbol('foo'); let s2 = Symbol('bar'); s1 // Symbol(foo) s2 // Symbol(bar) s1.toStri ...

  4. BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...

  5. BigDecimal 实际测试结果

    package com.zzzy; import java.math.BigDecimal; public class Test { public static void main(String[] ...

  6. git bash的使用

    一.创建本地版本控制仓库 cd e:   进入e盘 cd gitspace 进入gitspace文件夹 git init 将E:\gitspace初始化为本地版本控制仓库 Initialized em ...

  7. nltk: Tokenizing text into sentences

    安装 nltk pip install nltk 下载nltk_data 方法一: 通过客户端下载 import nltk nltk.download() 出现如下客户端,选择所需的包下载.(但由于网 ...

  8. C和C++语言&

    #include "stdafx.h"#include "iostream"#include "animal.h"using namespa ...

  9. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  10. Gibonacci number-斐波那契数列

    Description In mathematical terms, the normal sequence F(n) of Fibonacci numbers is defined by the r ...