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 ...
随机推荐
- Java中String的替换函数:replace与replaceAll的区别
例如有如下x的字符串 String x = "[kllkklk\\kk\\kllkk]"; 要将里面的"kk"替换为++,可以使用两种方法得到相同的结果 r ...
- 深度学习voc数据集图片resize
本人新写的3个pyhton脚本. (1)单张图片的resize: # coding = utf-8 import Image def convert(width,height): im = Image ...
- (转)ios error:unrecognized selector sent to class
转自:http://blog.itpub.net/12231606/viewspace-1081952/ 今天将app统计的.a静态库包含到一个app应用中,调试时报下面的错误: *** Termin ...
- $(window).load与$(document).ready的区别
刚好今天学了用jQuery实现瀑布流的用法,但加载时发现了一个小小的问题,那就是分别用$(window).load与$(document).ready实现加载时,$(document).ready的布 ...
- Prufer Code
1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without c ...
- Why are Eight Bits Enough for Deep Neural Networks?
Why are Eight Bits Enough for Deep Neural Networks? Deep learning is a very weird technology. It evo ...
- windows下启动mysql服务的命令行启动和手动启动方法
1.图形界面下启动mysql服务. 在图形界面下启动mysql服务的步骤如下: (1)打开控制面板->管理工具->服务,如下图所示: 可以看到Mysql服务目前的状态是未启动(未写已启动的 ...
- C语言实现线性表(链式存储方式)
#include <stdio.h> #include <stdlib.h> //提供malloc()原型 typedef struct LNode *List; typede ...
- NYOJ 221 Tree (二叉树)
题目链接 描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructi ...
- Attention is all you need 论文详解(转)
一.背景 自从Attention机制在提出之后,加入Attention的Seq2Seq模型在各个任务上都有了提升,所以现在的seq2seq模型指的都是结合rnn和attention的模型.传统的基于R ...