北邮校赛 H. Black-white Tree (猜的)

时间限制 1000 ms 内存限制 65536 KB

题目描述

Alice and Bob take turns to perform operations on a rooted tree of size n. The tree is rooted at node 1, with each node colored either black or white. In each turn a player can choose a black node and inverse the color of all nodes in the node's subtree. If any player can't perform the operation, he loses. Now Alice plays first, who will win if both players adopt the best strategy?

输入格式

The first line contains only one integer T(1T10), which indicates the number of test cases.
For each test case:

  • The first line contains an integer n(1n100000), indicating the size of the tree;
  • The second line contains n integers a1,a2,...,an(ai=0,1), representing the color of the ith node where 0 indicates white and 1 indicates black;
  • In the next n1 lines, each line contains two integers u,v(1u,vn), indicating there is an edge between node u and node v.

 

输出格式

For each test case, output Alice or Bob to show the winner.

输入样例

3
3
1 1 1
1 2
1 3
3
1 1 0
1 2
1 3
3
0 0 0
1 2
1 3

输出样例

Alice
Bob
Bob
【题意】给你一棵树,即每个节点的颜色,黑或者白,然后两个人玩游戏,每个人选择一个黑节点,并且翻转它的子树的颜色,问谁赢。
【分析】看似一道博弈题,但是不会啊,已看过的人还不少,于是队友猜想,这个树给出以后,直接从上往下模拟,是什么就是什么。
啪啪啪写了一发,还真就过了,不知道什么道理。。。
#include <bits/stdc++.h>
 
using namespace std;
const int MAXN = 1000005;
typedef long long int LL;
 
vector<int>G[MAXN];
int n;
int a[MAXN], lazy[MAXN],cnt;
void dfs(int u, int fa){
    lazy[u]+=lazy[fa];
    if(lazy[u]&1)a[u]^=1;
    if(a[u]) cnt++;
    for(int i = 0; i < (int)G[u].size(); i++){
        int v=G[u][i];
        if(v==fa)continue;
        if(a[u])lazy[v]++;
        dfs(v,u);
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        cnt=0;
        memset(lazy,0,sizeof lazy);
        for(int i=0;i<=n;i++)G[i].clear();
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 0, u, v; i < n-1; i++){
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(1,0);
        if(cnt&1)puts("Alice");
        else puts("Bob");
    }
    return 0;
}

 

 
posted @ 2017-04-30 10:43  贱人方  阅读(275)  评论(0编辑  收藏  举报