传送门: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

Problem Description
Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2,...,n, connected by n−1 bidirectional edges. The i-th vertex has the value of wi.
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.
Input
The first line of the input contains an integer T(1≤T≤20), denoting the number of test cases.
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.
Output
For each test case, print a single line containing a word, denoting the result. If Q wins, please print Q. If T wins, please print T. And if the game ends in a draw, please print D.
Sample Input
1
3
2 2 2
1 2
1 3

Sample Output
Q
Source
2018 Multi-University Training Contest 3

题意概括:

给出一棵有 N 个节点的树,给出每个节点的权值。

有Q ,T, D 三个人物。

Q 先在树里面取节点,要求是有边相连的节点只能选其一。

Q选完之后剩下的所有节点都属于 T。

比较 Q 和 T的节点权值异或和,如果 Q 的大则Q赢,反则 T 赢,平局输出 D

解题思路:

根据Q的选取条件,我们知道Q只能在这棵树上隔层取值。

那么我们可以把这棵树的节点分成两堆,第一堆是从第一层开始隔层取,每一层都把该层的节点全部取了。

那么如果这两堆不相等 Q 赢(他先选,选最大的那堆),如果两堆相等 平局。

因为如果出现两堆相等的情况,无论怎么交换节点,两个相同的值异或相同的值结果还是相等。

所以T不可能赢。

BFS跑一遍分成两堆即可。

AC code:

 1 #include <set>
 2 #include <map>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 #define INF 0x3f3f3f3f
 8 #define LL long long
 9 using namespace std;
10 const int MAXN = 1e5+10;
11 struct Edge
12 {
13     int v, nxt;
14 }edge[MAXN<<1];
15 int head[MAXN], cnt;
16 int w[MAXN];
17 int ans_a, ans_b;
18 void init()
19 {
20     memset(head, -1, sizeof(head));
21     ans_a = ans_b = 0;
22     cnt = 0;
23 }
24
25 void add(int from, int to)
26 {
27     edge[cnt].v = to;
28     edge[cnt].nxt = head[from];
29     head[from] = cnt++;
30 }
31
32
33
34 void bfs(int x, bool no)
35 {
36     if(no){
37         ans_a^=w[x];
38         for(int i = head[x]; i != -1; i = edge[i].nxt){
39             bfs(edge[i].v, !no);
40         }
41     }
42     else{
43         ans_b^=w[x];
44         for(int i =head[x]; i != -1; i = edge[i].nxt){
45             bfs(edge[i].v, !no);
46         }
47     }
48 }
49
50 int main()
51 {
52     int N, u, v;
53     int T_case;
54     scanf("%d", &T_case);
55     while(T_case--){
56         scanf("%d", &N);
57         init();
58         for(int i = 1; i <= N; i++){
59             scanf("%d", &w[i]);
60         }
61
62         for(int i = 1; i < N; i++){
63             scanf("%d %d", &u, &v);
64             add(u, v);
65         }
66         ans_a = ans_b = 0;
67         bfs(1, true);
68
69         if(ans_a == ans_b) puts("D");
70         else puts("Q");
71
72     }
73     return 0;
74 }

转载于:https://www.cnblogs.com/ymzjj/p/10295599.html

2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】相关推荐

  1. 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 ...

  2. Problem F. Grab The Tree HDU - 6324(树形dp+博弈)

    Little Q and Little T are playing a game on a tree. There are nn vertices on the tree, labeled by 1, ...

  3. Problem F. Grab The Tree博弈

    Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2 ...

  4. 2018秋c语言程序设计考试答案,2018年自学考试《C语言程序设计》模拟试题【四篇】...

    [导语]"一分耕耘一分收获",十载寒窗铸直了你挺拔的身姿,丰富的知识拉远你睿智的目光,岁月的流逝反衬出你娇美的容颜,奋斗的道路上,你的身影显得无比的昂扬.你与每一个成功拥抱,你的汗 ...

  5. F - LIS on Tree【二分OR权值线段树】

    传送门 atcoder F - LIS on Tree 题意 给定一颗树,求节点111到各个节点路径上的最长上升子序列的长度 分析 之前只知道对序列进行二分nlognnlognnlogn的做法 给定一 ...

  6. 2018河北计算机类单招试题,2018年河北单招数学(理科)模拟试题【含答案】

    2021年高职单招升学一对一咨询高职单招赵老师:17782071903(微信) 2018年河北单招数学(理科)模拟试题[含答案] 第ⅰ卷 一.选择题:本大题共12个小题,每小题5分,共60分.在每小题 ...

  7. 2018 China Collegiate Programming Contest - Jilin Site F - The Hermit HDU - 6560 思维

    链接Problem - 6560 题意 有n个站点每个站点可以发送完美信号 关于完美信号的定义 有i j k三个站点 分别保证 i<j<k dis(i,j)> dis(j,k) 并且 ...

  8. 2018年东北农业大学春季校赛 F wyh的集合【思维】

    链接:https://www.nowcoder.com/acm/contest/93/F 来源:牛客网 题目描述 你们wyh学长给你n个点,让你分成2个集合,然后让你将这n个点进行两两连接在一起,连接 ...

  9. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 4032  Solved: 1817 [Submi ...

最新文章

  1. 云炬Android开发笔记 5-8文件下载功能设计与实现
  2. 如何稀释 流事件 (如,onscroll、change、input、mouseover 等 事件)
  3. linux网络编程九:splice函数,高效的零拷贝
  4. 使用【Linux操作系统】必须掌握的基本命令
  5. JAVA设计模式初探之适配器模式(转)
  6. java.io.IOException: InvalidResourceRequestException: Invalid resource request
  7. 2020年4月中国编程语言排行榜程序员工资统计,人工智能工资大跌
  8. 查看某个进程的线程在干什么_有了多线程,为什么还要有协程?
  9. c改java_如何将一个c程序改写成JAVA程序
  10. [转载] Python中为什么len不是普通方法
  11. struts2与spring集成时,关于class属性及成员bean自动注入的问题
  12. 重装系统 2021年最新方法 win10纯净版本(官网方法)亲测有效
  13. php 函数名,php里函数名或者方法名前加 符号表示的意思
  14. 使用RaiDrive将NAS中的磁盘映射为本地磁盘
  15. 【Docker之Swarm详细讲解Swarm集群搭建管理节点工作节点Raft一致性协议overlay网络Docker结合Swarm部署WordPress个人博客实战】
  16. 【vue】vue中axios的使用及vue生命周期详解_07
  17. 计算机网络基础选择题
  18. 恭喜 SphereEx 联合创始人潘娟成为亚马逊云科技新晋 Data Hero
  19. 基于cocos2dx的横版动作游戏制作(一)
  20. 今天许多的家庭有计算机英语,用英语写我的家庭作文3篇

热门文章

  1. 学生成绩管理网站之——课程视频分享实现
  2. mysql-Mac终端下遇到的问题总结
  3. linux下rpm包和命令使用简介
  4. Codeforces Round #188 (Div. 1) B. Ants 暴力
  5. 设计模式学习每天一个——Factory模式 和 Abstract Factory模式
  6. 存储时间:从Symmetrix V-Max看高端存储的未来
  7. Integer to Roman 问题
  8. 精细化容量管理的设备成本优化之路
  9. ORA-01502: index ‘index_name' or partition of such index is in unusable state
  10. 通过Attached Property给控件绑定Command(二)