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
题解:由于是求异或,我们只需考虑每一位上的 1 的个数即可,比如:对于最高位,如果为偶数,那么小Q只需选一个或者不选即可那么小Q,小T最后该位上的数是相同的,如果为奇数,小Q选一个为必胜,因为小Q就选这一个,剩下都给小T,小T的最终结果必定小于小Q, 因此,我们只需要对每一位上的1的个数加一遍,如果有出现奇数个,则Q必胜,否者平局;
参考代码为:
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int w[maxn],u[maxn],v[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); int T,n;
cin>>T;
while(T--)
{
cin>>n;
bool flag=false;
for(int i=1;i<=n;i++) cin>>w[i];
for(int i=1;i<n;i++) cin>>u[i]>>v[i];
for(int i=0;i<32;i++)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
if(w[i]&1) cnt++;
w[i]>>=1;
}
if(cnt & 1)
{
cout<<"Q"<<endl;
flag=true;
break;
}
}
if(!flag) cout<<"D"<<endl;
} return 0;
}

  

2018HDU多校训练-3-Problem F. Grab The Tree的更多相关文章

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

  2. Problem F. Grab The Tree HDU - 6324

    题意:给出一棵n个节点的树,每个节点有一个权值,Q和T玩游戏,Q先选一些不相邻的节点,T选剩下的节点,每个人的分数是所选节点的权值的异或和,权值大的胜出,问胜出的是谁. 题解: 话说,这题后面的边跟解 ...

  3. HDU 6324.Problem F. Grab The Tree-博弈(思维) (2018 Multi-University Training Contest 3 1006)

    6324.Problem F. Grab The Tree 题目看着好难,但是题解说的很简单,写出来也很简单.能想出来就是简单的,想不出来就难(讲道理,就算是1+1的题目,看不出来就是难的啊). 和后 ...

  4. 2018HDU多校训练-3-Problem D. Euler Function

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's toti ...

  5. 牛客网多校训练第一场 F - Sum of Maximum(容斥原理 + 拉格朗日插值法)

    链接: https://www.nowcoder.com/acm/contest/139/F 题意: 分析: 转载自:http://tokitsukaze.live/2018/07/19/2018ni ...

  6. 2018HDU多校训练-3-Problem M. Walking Plan

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331 Walking Plan  Problem Description There are n inte ...

  7. 2018HDU多校训练-3-Problem G. Interstellar Travel

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6325                                   Interstellar Tra ...

  8. 2018HDU多校训练一 K - Time Zone

    Chiaki often participates in international competitive programming contests. The time zone becomes a ...

  9. 2018HDU多校训练一 D Distinct Values

    hiaki has an array of nn positive integers. You are told some facts about the array: for every two e ...

随机推荐

  1. hdu 1880 魔咒词典(双hash)

    魔咒词典Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. 领扣(LeetCode)翻转二叉树 个人题解

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题  ...

  3. W5500设计方案

    W5500是韩国一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,W5500同时也是一颗工业级以太网控制芯片,最近发现我们国内也有和W5500 芯片一样芯片 介绍给大家 如下图:

  4. 20191107-3 beta week 2/2 Scrum立会报告+燃尽图 02

    此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/9955] 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 ...

  5. 2019-11-28:ssrf基础学习,笔记

    ssrf服务端请求伪造ssrf是一种由恶意访问者构造形成由服务端发起请求的一个安全漏洞,一般情况下,ssrf访问的目标是从外网无法访问的内部系统,正式因为它是由服务端发起的,所以它能请求到它相连而外网 ...

  6. Java架构师必知:什么是单点登录,主要会应用于哪些场景?

    单点登录在大型网站里使用得非常频繁,例如,阿里旗下有淘宝.天猫.支付宝,阿里巴巴,阿里妈妈,阿里妹妹等网站,还有背后的成百上千的子系统,用户一次操作或交易可能涉及到几十个子系统的协作,如果每个子系统都 ...

  7. String对象常量池

    对象池的主要目的是实现数据的共享处理, 在java之中对象池可以分为两种: 1.静态常量池 :指*.class加载时会自动将此程序之中保存的 字符串.普通的常量.类和方法的信息等全部经行分配 2.运行 ...

  8. flask实现验证码并验证

    效果图: 点击图片.刷新页面.输入错误点击登录时都刷新验证码 实现步骤: 第一步:先定义获取验证码的接口 verificationCode.py #验证码 @api.route('/imgCode') ...

  9. 运维常用 MySQL 命令

    目录 设置用户密码 方法一 方法二 丢失root密码找回 添加用户 设置用户密码 方法一 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewPass ...

  10. 我的第一个 python 爬虫脚本

    #!/usr/bin/env python# coding=utf-8import urllib2from bs4 import BeautifulSoup #res = urllib.urlopen ...