zoukankan      html  css  js  c++  java
  • 2017BUPT校赛 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

    每棵树的“奇偶性”都是确定的,只需看最初的树是奇还是偶即可。

    对于一棵树,如果根节点为1,那么如果以根结点的子节点为根的子树中有偶数个偶树,这个树就是奇树;如果根结点为0,那么如果以根节点的子节点为根的子树中有奇数个奇树,这个树就是奇树。其余情况为偶树。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <stack>
    12 #define mp make_pair
    13 typedef long long ll;
    14 typedef unsigned long long ull;
    15 const int MAX=1e5+5;
    16 const int INF=1e9+5;
    17 using namespace std;
    18 typedef pair<int,int> pii;
    19 vector <int>edge[MAX];
    20 int x,y;
    21 int t,n;
    22 int a[MAX];
    23 int check(int x,int from)
    24 {
    25     int b[2]={0};
    26     int to;
    27     for(int i=0;i<edge[x].size();i++)
    28     {
    29         to=edge[x][i];
    30         if(to==from)
    31             continue;
    32         b[check(to,x)]^=1;
    33     }
    34     if((a[x]&&!b[0])||(!a[x]&&b[1]))
    35         return 1;
    36     else return 0;
    37 }
    38 int main()
    39 {
    40     scanf("%d",&t);
    41     while(t--)
    42     {
    43         scanf("%d",&n);
    44         for(int i=1;i<=n;i++)
    45             scanf("%d",&a[i]),edge[i].clear();
    46         for(int i=1;i<n;i++)
    47         {
    48             scanf("%d%d",&x,&y);
    49             edge[x].push_back(y);edge[y].push_back(x);
    50         }
    51         if(check(1,0))
    52             printf("Alice
    ");
    53         else
    54             printf("Bob
    ");
    55     }
    56 }
  • 相关阅读:
    数组和切片
    if else,for循环,switch语句
    数据库介绍以及MySQL数据库的使用
    Django Rest Framwork的认证组件 权限组件以及频率组件
    Django Rest Frawwork框架的CBV FBV分析
    事务介绍
    celery介绍
    多道技术 进程 线程 协程 GIL锁 同步异步 高并发的解决方案 生产者消费者模型
    win10安装mysql8.0版本
    安装VMware Tools的注意事项
  • 原文地址:https://www.cnblogs.com/quintessence/p/6708730.html
Copyright © 2011-2022 走看看