本文链接:http://www.cnblogs.com/Ash-ly/p/5932748.html

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883

思路:

  先判断原图是否是欧拉回路或者欧拉通路.是的话如果一个点的度数除以2是奇数则可以产生一个XOR贡献值.之后如果是欧拉通路, 则答案是固定的,起点和终点需要多产生一次贡献值. 如果是欧拉回路, 则需要枚举起点.

代码:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath> using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXE = ;
int a[MAXN + ], deg[MAXN + ], pre[MAXN + ], t, n, m; int Find(int x) { return x == pre[x] ? x : pre[x] = Find(pre[x]); } void mix(int x, int y) {
int fx = Find(x), fy = Find(y);
if(fx != fy) pre[fx] = fy;
} int isEulr(int n) {
int cnt1 = , cnt2 = ;
for(int i = ; i <= n; i++) {
if(deg[i] & ) cnt1++;
if(pre[i] == i) cnt2++;
}
if( (cnt1 == || cnt1 == ) && cnt2 == ) return cnt1; //cnt1 为 0 代表欧拉回路, 为 2 代表欧拉通路
return -;
} int main(){
scanf("%d", &t);
while(t--) {
memset(a, , sizeof(a));
memset(deg, , sizeof(deg)); scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= n; i++) pre[i] = i;
int u, v, k;
for(int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
deg[u]++, deg[v]++;
mix(u, v);
}
if( (k = isEulr(n) ) >= ) {
int ans = ;
for(int i = ; i <= n; i++) ans ^= ( (deg[i] / ) & ? a[i] : ) ;
if(k == )for(int i = ; i <= n; i++) { if(deg[i] & ) ans ^= a[i]; }//欧拉通路,起点和终点需要多XOR一次
else for(int i = ; i <= n; i++) ans = max(ans, ans ^ a[i]); //欧拉回路, 枚举下起点
printf("%d\n", ans);
}
else printf("Impossible\n");
}
return ;
}

HDU5883 The Best Path(欧拉回路 | 通路下求XOR的最大值)的更多相关文章

  1. hdu5883 The Best Path(欧拉路)

    题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...

  2. 1. while循环(当循环) 2. do{}while()循环 3. switch cose(多选一) 例子:当选循环下求百鸡百钱 用 switch cose人机剪刀石头布

    1. while循环: 当选循环下求百鸡百钱:如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  3. js求数组的最大值--奇技淫巧和笨方法

    写这篇文章的原因 我目前做的项目很少用到算法,于是这方面的东西自然就有点儿生疏.最近的一次编码中遇到了从数组中获取最大值的需求,当时我不自觉的想到了js的sort()函数,现在想来真是有些“罪过”,当 ...

  4. leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)

    题目描述: In a given integer array nums, there is always exactly one largest element. Find whether the l ...

  5. 【编程题目】一个整数数组,长度为 n,将其分为 m 份,使各份的和相等,求 m 的最大值★★ (自己没有做出来!!)

    45.雅虎(运算.矩阵): 2.一个整数数组,长度为 n,将其分为 m 份,使各份的和相等,求 m 的最大值 比如{3,2,4,3,6} 可以分成 {3,2,4,3,6} m=1; {3,6}{2,4 ...

  6. JS创建一个数组1.求和 2.求平均值 3.最大值 4.最小值 5.数组逆序 6.数组去重 0.退出

    rs = require("readline-sync"); let arr = []; console.log("请输入数组的长度:"); let arr_l ...

  7. c# 求数组的最大值

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 【c语言】不用大与小与号,求两数最大值

    // 不用大与小与号,求两数最大值 #include <stdio.h> int max(int a, int b) { int c = a - b; int d = 1 << ...

  9. [hdu3949]XOR(线性基求xor第k小)

    题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...

随机推荐

  1. OpenCV---图像梯度

    图像梯度 推文:[OpenCV入门教程之十二]OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑 图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个 ...

  2. ios 替换字符串中的部分字符串

    1.使用NSString中的stringByTrimmingCharactersInset:[NSCharacterSet whitespaceCharacterSet]方法去掉左右两边的空格: 2. ...

  3. SQL语句(二十二)—— 权限授予和回收(作业练习)

    CREATE TABLE course ( Cno ) NOT NULL, Cname ) DEFAULT NULL, Cpno ) DEFAULT NULL, Ccredit smallint DE ...

  4. JavaScript arguments你不知道的秘密

    (function test(x){ x=10; console.log(arguments[0], x); //undefined, 10 })(); (function test(x){ x=10 ...

  5. windos下创建软链接,附Linux下创建软链接

    用过好多次老是忘记: 写在这里忘了就来看下 Windows下(win7) mklink /D D:\phpStudy\WWW\yii\school\teacher\web\uploads\public ...

  6. 利用media query写响应式布局

    最近才接触到响应式布局的概念,之前用到的bootstrap就是一种响应式布局的框架.学习的时候参考了http://blog.csdn.net/shoyer/article/details/829301 ...

  7. WordPress404页面自定义

    不知道大家是怎么设计404页面,个性的404可以为网站增色不少,wordpress设置404是在主题里面的404.php页面上,当然比如你用Apache.nginx等服务器,你可以自己建一个单页,内容 ...

  8. KS(Kolmogorov-Smirnov)(转)

    来源:https://blog.csdn.net/u013421629/article/details/78217498 KS(Kolmogorov-Smirnov):KS用于模型风险区分能力进行评估 ...

  9. ThinkPHP的运行流程-2

    Thinkphp为了提高编译的效率,第一次运行的时候thinkphp会把文件全部编译到temp目录下的~runtime.php文件,在第二次运行的时候会直接读取这个文件.所以我们在线下自己写代码测试的 ...

  10. python基础之常用的高阶函数

    前言 高阶函数指的是能接收函数作为参数的函数或类:python中有一些内置的高阶函数,在某些场合使用可以提高代码的效率. map() map函数可以把一个迭代对象转换成另一个可迭代对象,不过在pyth ...