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 ...
随机推荐
- [USACO14MAR] Counting Friends
题目描述 Farmer John's N cows (2 <= N <= 500) have joined the social network "MooBook". ...
- easyui 更改dialog弹出的位置
方法一: 在弹出dialog的时候不用$('#dialogDiv').dialog('open');打开.用$('#dialogDiv').window('open');打开.再用window的res ...
- cin.getline()与getline()
C++中有两个getline函数, cin.getline()与getline() 这两个函数相似,但是 这两个函数分别定义在不同的头文件中. cin.getline()属于istream流,而 ...
- Spark RDD中的aggregate函数
转载自:http://blog.csdn.net/qingyang0320/article/details/51603243 针对Spark的RDD,API中有一个aggregate函数,本人理解起来 ...
- Centos7环境下消息队列之ActiveMQ实战
Activemq介绍 对于消息的传递有两种类型: 一种是点对点的,即一个生产者和一个消费者一一对应: 另一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收. JMS定义了 ...
- java中error和exception
异常是指程序运行时发生的错误. Throwable是所有异常的父类,它有两个子类:Error和Exception. 1.Error表示程序在运行期间发生了非常严重的错误,并且该错误是不可恢复的.Err ...
- 【POJ】3233 Matrix Power Series
[算法]二分+矩阵快速幂 [题意]给定矩阵A和整数k,MOD,求A^0+A^1+A^2+...+A^k. [题解] 定义题目要求的答案为f(n),即: $$f_n=\sum_{i=0}^{n}A^i$ ...
- call_user_func 具体使用方法,实例说明
<?php class Person{ public $name="jack"; public static function say(){ echo "ok&qu ...
- 014 JVM面试题
转自:http://www.importnew.com/31126.html 本文从 JVM 结构入手,介绍了 Java 内存管理.对象创建.常量池等基础知识,对面试中 JVM 相关的基础题目进行了讲 ...
- SpringMVC_HelloWorld_03
通过注解的方式实现一个简单的HelloWorld. 源码 一.新建项目 同SpringMVC_HelloWorld_01 不同的是springmvc配置文件的命名和路径,此处为src/springmv ...