zoukankan      html  css  js  c++  java
  • 北邮校赛 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;
    }
     
  • 相关阅读:
    T4模板使用记录,生成Model、Service、Repository
    sortablejs + vue的拖拽效果 列表个数不固定 刷新后保持拖拽后的效果
    vue获取input焦点,弹框后自动获取input焦点
    vue proxy 跨域代理
    vue 同步 $nextTick setTimeout 执行的顺序
    js手写日历插件
    js数组随机排序
    vue自定义插件
    elementui 自定义表头 renderHeader的写法 给增加el-tooltip的提示
    awit的用法,等待执行结果
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6789014.html
Copyright © 2011-2022 走看看