F

https://vjudge.net/contest/310812#problem/F

给定一棵n 个点的树,每个点有权值。两个人玩游戏,先
手需要占领若干不相邻的点,然后后手占领剩下所有点。
每个人的得分为占领的点权异或和,分高的获胜。问最优策
略下游戏的结果。
1 ≤ n ≤ 105。
Shortest judge solution: 235 bytes

### 思路
并不能算是博弈的题
分析后手最好的结果就是平局,不可能获胜的

sum 为所有点权的异或和,A 为先手得分,B 为后手得分。
1)sum = 0只能是平局
2)sum不为零 二进制下最高位1
若先手拿走任意一个那一位为1 的点,则B 该位为0 A必胜

### 代码

#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <stack>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <iterator>
#include <set>
#include <map>
#include <utility>
#include <iomanip>
#include <ctime>
#include <sstream>
#include <bitset>
#include <deque>
#include <limits>
#include <numeric>
#include <functional>

#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define mod 1000000007
#define sort(a,n,int) sort(a,a+n,less<int>())
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)
using namespace std;

typedef long long ll;
typedef char ch;
typedef double db;  

const int maxn=1e5+10;
//ll a[maxn];
int aa[maxn];

int gcd(int a,int b){
    if(a<b)swap(a,b);
    if(a%b==0)return b;
    else gcd(b,a%b);
}

int main()
{
    int t, n;
    cin >> t;
    int w[maxn] = {0}, u[maxn] = {0}, v[maxn] = {0};
    int flag = 0;
    int ans = 0;
    
    while(t--) {
        cin >> n;
        for(int i = 0; i < n; i++)        cin >> w[i];
        for(int i = 0; i < n-1; i++)    cin >> u[i] >> v[i];
        for(int i = 0; i < 32; i++){
            ans = 0;
            for (int j = 0; j < n; j++){
                if (w[j] & 1)ans++;
                w[j] >>= 1;
            }
            if (ans & 1){
                flag = 1;
                break;
            }
        }
        if (flag)    cout<<'Q'<<endl;
        else        cout<<'D'<<endl;
    }
    return 0;
}
View Code

作者:YukiRinLL

出处:YukiRinLL的博客--https://www.cnblogs.com/SutsuharaYuki/

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者所有,欢迎转载,但请保留该声明。

原文地址:https://www.cnblogs.com/SutsuharaYuki/p/11224184.html