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 ...
随机推荐
- Error : getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
环境 阿里云 centos7 node v8.11.3 npm 5.6.0 错误 npm update 解决 ping registry.npmjs.org 发现https://registry.np ...
- 在服务器上运行Jar包
在服务器上运行Jar包 并且该Jar包依赖其他的Jar文件的时候,采用如下格式 java -Djava.ext.dirs=你依赖的Jar文件路径 -jar 你要运行的Jar文件 包名+类名 例如: j ...
- JS笔记-强化版1
1.函数:可以理解为-命令,做一些事~~ function abc(){ // 肯定不会主动执行的! …… } 直接调用:abc(); 事件 ...
- 多例模式,保证实例的唯一性,仅适用于form窗体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 782C. Andryusha and Colored Balloons DFS
Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...
- Python学习笔记(三十三)常用内置模块(2)collections_namedtuple_deque_defaultdict_OrderedDict_Counter
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431953239 ...
- jQuery 页面加载初始化
jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...
- 天梯赛 L2-006 树的遍历 (二叉树)
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树中结点的个数.第二行给出其后序遍历序 ...
- King's Quest POJ - 1904 匈牙利算法的思想+tarjan缩点+染色
题目链接:https://cn.vjudge.net/problem/POJ-1904 自己一开始的想法,打算用匈牙利算法实现,找二分图的最大匹配.但是打了打发现,不太好实现.原因如下:匈牙利算法是不 ...
- javascript复习笔记
/* Javascript:用来在页面中编写特效,和HTML.CSS一样都是有浏览器解析 Javascript语言: 一.JS如何运行(javascript,jscript,vbscript,appl ...