H. Black-white Tree 2017- BUPT Collegiate Programming Contest - sync

时间限制 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(1≤T≤10), which indicates the number of test cases.
For each test case:

  • The first line contains an integer n(1≤n≤100000), 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 n−1 lines, each line contains two integers u,v(1≤u,v≤n), 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 = ;
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]&)a[u]^=;
if(a[u]) cnt++;
for(int i = ; 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=;
memset(lazy,,sizeof lazy);
for(int i=;i<=n;i++)G[i].clear();
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = , u, v; i < n-; i++){
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(,);
if(cnt&)puts("Alice");
else puts("Bob");
}
return ;
}
 

北邮校赛 H. Black-white Tree (猜的)的更多相关文章

  1. 北邮校赛 F. Gabriel's Pocket Money(树状数组)

    F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...

  2. 北邮校赛 I. Beautiful Array(DP)

    I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...

  3. PKU2018校赛 H题 Safe Upper Bound

    http://poj.openjudge.cn/practice/C18H 题目 算平均数用到公式\[\bar{x}=\frac{x_1+x_2+x_3+\cdots+x_n}{n}\] 但如果用in ...

  4. 2019湘潭校赛 H(dp)

    题目传送 dp是常规的:\(m^2\)的预处理:把位置存进vector然后\(O(1)\)算出想要的:WA点:要注意特意设置一下val[i][v.size()]=0,即全天都放鸽子则花费时间为0. # ...

  5. Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )

    题目链接 题意 : 中文题.点链接 分析 : 有道题是问你不断求前缀和后的结果 Click here 这道题问的是逆过程 分析方法雷同.可参考 Click here ----------------- ...

  6. [BNUZOJ1261][ACM][2016北理校赛]方块消除(栈,字符串)

    玩过方块消除游戏吗?现在规定当有两个或两个以上相邻且颜色相同的方块在一起的时候,它们就会产生消除反应.当存在多个消除反应同时产生时,最下的反应先执行.现在只给你其中一列,求最后剩下的方块结果. 输入要 ...

  7. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  8. 北邮14&18年软院机试【参考】答案

    2014 Problem A. 奇偶求和 题目描述: 给定N个数,分别求出这N个数中奇数的和以及偶数的和. 输入格式 第一行为测试数据的组数T(1<=T<=50).请注意,任意两组测试数据 ...

  9. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

随机推荐

  1. 搜索:BFS

    如果题目真的要考察宽度优先搜索,那么这类题目往往具有比较大的编码难度,换个说法,就是细枝末节特别多,状态特别复杂.. 剥茧抽丝,这里以一个比较“裸”的BFS作为例子,了解一下实现BFS的一些规范. 直 ...

  2. Linux之防火墙与端口

    1.关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放.下面是命令实现: iptables -P INPUT DROPiptables -P FORWARD DROPiptable ...

  3. consul windows安装

    Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2.0 的协议进行开 ...

  4. 【poj2464】树状数组

    这道题..太特么多细节了.. 题意:在平面直角坐标系中给你N个点,stan和ollie玩一个游戏,首先stan在竖直方向上画一条直线,该直线必须要过其中的某个点,然后ollie在水平方向上画一条直线, ...

  5. 51nod 1363 最小公倍数之和 ——欧拉函数

    给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30,6,加在一起 = 66. 由于结果很大,输出Mod 1000 ...

  6. python初步学习-python数据类型之strings(字符串)

    数据类型-字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号(''或者"")来创建字符串 var1 = 'Hello World!' var2 = "P ...

  7. react组件之间的几种通信情况

    组件之间的几种通信情况 父组件向子组件通信 子组件向父组件通信 跨级组件通信 没有嵌套关系组件之间的通信 1,父组件向子组件传递 React数据流动是单向的,父组件向子组件通信也是最常见的;父组件通过 ...

  8. H题 hdu 2520 我是菜鸟,我怕谁

    题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=2520 我是菜鸟,我怕谁 Time Limit: 2000/1000 MS (Java/Others)  ...

  9. mysql中的enum型

    enum设置后 值只能是给出的值中的其中一个 mysql> create table enum(e enum('1','2','3','4','5','6','7','8','9','10')) ...

  10. Linux-进程间通信(三): 共享内存

    1. 共享内存: 共享内存方式可以在多个进程直接共享数据,因为其直接使用内存,不要多余的拷贝,是速度最快的IPC方式: 共享内存有两种实现方式,使用mmap和shm方式,如下图: (1) mmap方式 ...