2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324
Problem F. Grab The Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1234 Accepted Submission(s): 779
In this game, Little Q needs to grab some vertices on the tree. He can select any number of vertices to grab, but he is not allowed to grab both vertices that are adjacent on the tree. That is, if there is an edge between x and y, he can't grab both x and y. After Q's move, Little T will grab all of the rest vertices. So when the game finishes, every vertex will be occupied by either Q or T.
The final score of each player is the bitwise XOR sum of his choosen vertices' value. The one who has the higher score will win the game. It is also possible for the game to end in a draw. Assume they all will play optimally, please write a program to predict the result.
In each test case, there is one integer n(1≤n≤100000) in the first line, denoting the number of vertices.
In the next line, there are n integers w1,w2,...,wn(1≤wi≤109), denoting the value of each vertex.
For the next n−1 lines, each line contains two integers u and v, denoting a bidirectional edge between vertex u and v.
题意概括:
给出一棵有 N 个节点的树,给出每个节点的权值。
有Q ,T, D 三个人物。
Q 先在树里面取节点,要求是有边相连的节点只能选其一。
Q选完之后剩下的所有节点都属于 T。
比较 Q 和 T的节点权值异或和,如果 Q 的大则Q赢,反则 T 赢,平局输出 D
解题思路:
根据Q的选取条件,我们知道Q只能在这棵树上隔层取值。
那么我们可以把这棵树的节点分成两堆,第一堆是从第一层开始隔层取,每一层都把该层的节点全部取了。
那么如果这两堆不相等 Q 赢(他先选,选最大的那堆),如果两堆相等 平局。
因为如果出现两堆相等的情况,无论怎么交换节点,两个相同的值异或相同的值结果还是相等。
所以T不可能赢。
BFS跑一遍分成两堆即可。
AC code:
#include <set>
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
struct Edge
{
int v, nxt;
}edge[MAXN<<];
int head[MAXN], cnt;
int w[MAXN];
int ans_a, ans_b;
void init()
{
memset(head, -, sizeof(head));
ans_a = ans_b = ;
cnt = ;
} void add(int from, int to)
{
edge[cnt].v = to;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void bfs(int x, bool no)
{
if(no){
ans_a^=w[x];
for(int i = head[x]; i != -; i = edge[i].nxt){
bfs(edge[i].v, !no);
}
}
else{
ans_b^=w[x];
for(int i =head[x]; i != -; i = edge[i].nxt){
bfs(edge[i].v, !no);
}
}
} int main()
{
int N, u, v;
int T_case;
scanf("%d", &T_case);
while(T_case--){
scanf("%d", &N);
init();
for(int i = ; i <= N; i++){
scanf("%d", &w[i]);
} for(int i = ; i < N; i++){
scanf("%d %d", &u, &v);
add(u, v);
}
ans_a = ans_b = ;
bfs(, true); if(ans_a == ans_b) puts("D");
else puts("Q"); }
return ;
}
2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】的更多相关文章
- 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
- 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...
- 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- Problem F. Grab The Tree HDU - 6324
题意:给出一棵n个节点的树,每个节点有一个权值,Q和T玩游戏,Q先选一些不相邻的节点,T选剩下的节点,每个人的分数是所选节点的权值的异或和,权值大的胜出,问胜出的是谁. 题解: 话说,这题后面的边跟解 ...
- HDU 6324.Problem F. Grab The Tree-博弈(思维) (2018 Multi-University Training Contest 3 1006)
6324.Problem F. Grab The Tree 题目看着好难,但是题解说的很简单,写出来也很简单.能想出来就是简单的,想不出来就难(讲道理,就算是1+1的题目,看不出来就是难的啊). 和后 ...
- HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...
随机推荐
- Opencv中图像的遍历与像素操作
Opencv中图像的遍历与像素操作 OpenCV中表示图像的数据结构是cv::Mat,Mat对象本质上是一个由数值组成的矩阵.矩阵的每一个元素代表一个像素,对于灰度图像,像素是由8位无符号数来表示(0 ...
- <td>标签scope属性
HTML <td> 标签的 scope 属性 HTML <td> 标签 实例 下面的例子把两个 th 元素标识为列的表头,把两个 td 元素标识为行的表头: <table ...
- 提示"No 'Access-Control-Allow-Origin' header"及Spring 中解决跨域问题
问题描述 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://12 ...
- 记一次Ubuntu 16.04 server安装中的坑
最近博主搞了一台迷你主机,又刚好有时间去折腾,所以我打算把这台机子打造成一台迷你服务器,用来跑跑爬虫.挂挂网站 介于我我这台机子的配置比较垃圾(intel J1900+4G+64G),跑起Window ...
- 互联网轻量级框架SSM-查缺补漏第八天(MyBatis插件plugin使用及原理)
简言:今天进行第八天的记录(只是写了八天).有的时候看的多,有的时候看的少,看的少的时候就攒几天一起写了.而今天这个插件我昨天写了一下午,下班没写完就回去了,今天把尾收了,再加上一个过程图方便下面原理 ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- 基础架构之Mongo
项目需求中,有些需求的数据是不必长时间持久化或一些非结构化设计,这时可以考虑用Mongo作为存储,具体介绍介绍详见官方 https://www.mongodb.com,这篇文章主要介绍安装及启用身份认 ...
- numpy用法介绍-未完待续
简介 NumPy(Numerical Python简称) 是高性能科学计算和数据分析的基础包 为什么使用? 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元 ...
- Android 友盟统计的集成与使用(包含多渠道打包配置)
前言 app上线后,一般公司都希望跟踪app在市场上的使用情况.包括新增用户.活跃用户.渠道信息.错误信息等,还有例如商城类的app,需要跟踪用户最喜欢浏览哪种类型的店铺或商品.这些都可以通过集成友盟 ...
- KLEE错误汇报一:One phenomenon after the execution using klee
https://github.com/klee/klee/issues/649#issuecomment-300424204 Hi, all, If you write test.c wit ...