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. Item 9 覆盖equals时总要覆盖hashCode

    为什么覆盖equals时,总要覆盖hashCode?   原因是,根据Object规范: 如果两个对象根据equals(Object)方法比较是相等的,那么调用这两个对象中任意一个对象的hashCod ...

  2. 用一个时钟在FPGA中计算直方图

    直方图对数字数据的分析通常是一种有用的工具.不过,要从一个直方图获得可靠的结果,必须获得大量数据,通常是要10万到100万个点.如果需要分析一个ADC的数字输出,可以采用一片FPGA(图1). 图中显 ...

  3. lua 闪电特效

    闪电特效 根据不同的起点 终点 资源做倾斜 拉伸 ,主要是计算倾斜角度. function ZyLight:show(params) local params = params or {} local ...

  4. hibernate单列的多值查询

    比如你的表主键是id,你要删除id 是 34,56,99 这样的.. uid是拼好的 比如 '34','56','99' ,以前我会这样写 String queryString = "upd ...

  5. httpd -v command not found

    使用 find / -name "apachectl"查找文件目录下执行 ./apachectl -v

  6. NodeJS中Buffer模块详解

    一,开篇分析 所谓缓冲区Buffer,就是 "临时存贮区" 的意思,是暂时存放输入输出数据的一段内存. JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一 ...

  7. mybatis 显示 sql日志

    #项目日志logging.level.com.zhang.com=debug #mybatis sql相关日志显示logging.level.org.mybatis.spring=DEBUGloggi ...

  8. 简谈const限定符

    const修饰的数据类型是常量类型,常量类型的对象和变量在定义初始化后是不能被更新的.其实只用记住这一个概念,就可以明白const操作对象的方法. 1)定义const常量 最简单的: const in ...

  9. android 内核调试

    这篇文档给出使用android emulator 和 arm-linux-androideabi-gdb 调试 android kernel 的方法 1. checkout goldfish 源码: ...

  10. Linux NAPI/非NAPI

    本文主要介绍二层收包流程,包括NAPI与非NAPI方式: NAPI:数据包到来,第一个数据包产生硬件中断,中断处理程序将设备的napi_struct结构挂在当前cpu的待收包设备链表softnet_d ...