【题目】
Problem F. Grab The TreeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 668 Accepted Submission(s): 450 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 .
Input The first line of the input contains an integer T(1≤T≤20) , denoting the number of test cases.
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 |
【题解】
题意:给定n个节点的权值与n-1条边,要求先手Q只能取不相邻的点,剩下的点归D,谁选取的点的权值异或和最大谁获胜。
思路:我们可以把所有的点分成两个点集,如果这两个点集异或和相等,就是后手赢,否则先手赢。又因为异或的性质a^a=0,所以全部的权值异或最终结果为0就是后手赢,否则先手赢。
【代码】
#include<stdio.h>
main()
{
int a[100010];
int t,n; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int x,y;
for(int i=1;i<n;i++)
scanf("%d%d",&x,&y);
int ans=a[1];
for(int i=2;i<=n;i++)
ans^=a[i];
if(ans==0) printf("D\n");
else printf("Q\n");
}
}