Problem F. Grab The Tree HDU

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

题解:

  话说,这题后面的边跟解的过程半毛钱关系没有,但是自己就是想不到,这博弈。。。

sum为所有点权的异或和,A为先手得分,B为后手得分。

  若sum=0,则A=B,故无论如何都是平局。

  否则考虑sum二进制下最高的1所在那位,一定有奇数个点那一位为1。若先手拿走任意一个那一位为1的点,则B该位为0,故先手必胜。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int n,ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            int a;
            scanf("%d",&a);
            ans^=a;
        }
        for(int i=0;i<n-1;i++){
            int  u,v;
            scanf("%d%d",&u,&v);
        }
        if(ans==0) cout<<"D"<<endl;
        else cout<<"Q"<<endl;
    }
}
View Code
原文地址:https://www.cnblogs.com/astonc/p/9991644.html