HDU5883 The Best Path(欧拉回路 | 通路下求XOR的最大值)
本文链接: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的最大值)的更多相关文章
- hdu5883 The Best Path(欧拉路)
题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...
- 1. while循环(当循环) 2. do{}while()循环 3. switch cose(多选一) 例子:当选循环下求百鸡百钱 用 switch cose人机剪刀石头布
1. while循环: 当选循环下求百鸡百钱:如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- js求数组的最大值--奇技淫巧和笨方法
写这篇文章的原因 我目前做的项目很少用到算法,于是这方面的东西自然就有点儿生疏.最近的一次编码中遇到了从数组中获取最大值的需求,当时我不自觉的想到了js的sort()函数,现在想来真是有些“罪过”,当 ...
- 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 ...
- 【编程题目】一个整数数组,长度为 n,将其分为 m 份,使各份的和相等,求 m 的最大值★★ (自己没有做出来!!)
45.雅虎(运算.矩阵): 2.一个整数数组,长度为 n,将其分为 m 份,使各份的和相等,求 m 的最大值 比如{3,2,4,3,6} 可以分成 {3,2,4,3,6} m=1; {3,6}{2,4 ...
- JS创建一个数组1.求和 2.求平均值 3.最大值 4.最小值 5.数组逆序 6.数组去重 0.退出
rs = require("readline-sync"); let arr = []; console.log("请输入数组的长度:"); let arr_l ...
- c# 求数组的最大值
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【c语言】不用大与小与号,求两数最大值
// 不用大与小与号,求两数最大值 #include <stdio.h> int max(int a, int b) { int c = a - b; int d = 1 << ...
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
随机推荐
- K8S dashboard 创建只读账户
1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...
- Final类和Final方法
终止继承 Final类 当关键字final用来修饰类时,其含义是该类不能在派生子类.换句话说,任何其他类都不能继承用final修饰的类,即使该类的访问限制为public类型,也不能被继承:否则,将编译 ...
- 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E
E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- JAVA多线程基础学习三:volatile关键字
Java的volatile关键字在JDK源码中经常出现,但是对它的认识只是停留在共享变量上,今天来谈谈volatile关键字. volatile,从字面上说是易变的.不稳定的,事实上,也确实如此,这个 ...
- C#为何不推荐在构造函数中访问虚成员
如果在一个类中定义了虚属性或者虚方法,又在构造函数中访问了这个虚属性或方法,此时VisualStudio是不会给出警告,并且编译也没有问题,但是如果安装了Resharper插件则会给出警告提示:&qu ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- NYOJ 138 找球号(二) (哈希)
题目链接 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,还有一个空箱子,现在有两种动作:一种是&qu ...
- [Gym-100625J] 搜索
题目链接:https://cn.vjudge.net/problem/Gym-100625J 具体思路:首先,具体思路是两个人一起走到一个点,然后两个人按照同样的道路走出去,听了别人的思路,还有一种特 ...
- webgote的例子(3)Sql注入(SearchPOST)
Sql注入(Search/POST) (本章内容):post的方式进行注入 今天来讲一下sql注入的另一个例子(post) 上一个用的是get请求的方法将我们的参数传到服务器进行执行 上图中的标红是需 ...
- Linux内核中的常用宏container_of其实很简单【转】
转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...