天天看點

HDU 6324 Problem F. Grab The Tree(博弈,異或)

【題目連結】

http://acm.hdu.edu.cn/showproblem.php?pid=6324

題目意思

Q與T玩遊戲,在一棵樹上每個節點都有一個權值,Q能取任意個節點,但是這些節點不能相連,剩下的都歸T。比較兩個人節點權值全部的異或和。如果相同輸出’D’

解題思路

根據異或的性質Q異或T會等于全部異或和。是以Q隻要取異或總和最高為為1的那部分。剩下肯定比他小。但如果全部異或為0那麼兩人就相同了。

代碼部分

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
#include <math.h>
#include <set>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int mod = +;
const int N = +;
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,a,u,v;
        scanf("%d",&n);
        int s = ;
        for (int i = ; i < n; i++)
        {
            scanf("%d",&a);
            s ^= a;
        }
        for (int i = ; i < n-; i++)
            scanf("%d %d",&u,&v);
        if (s == )
            printf("D\n");
        else printf("Q\n"); 
    }
    return ;
}
           

繼續閱讀