北邮校赛 H. Black-white Tree (猜的)
H. Black-white Tree 2017- BUPT Collegiate Programming Contest - sync
题目描述
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 (猜的)的更多相关文章
- 北邮校赛 F. Gabriel's Pocket Money(树状数组)
F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...
- 北邮校赛 I. Beautiful Array(DP)
I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...
- 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 ...
- 2019湘潭校赛 H(dp)
题目传送 dp是常规的:\(m^2\)的预处理:把位置存进vector然后\(O(1)\)算出想要的:WA点:要注意特意设置一下val[i][v.size()]=0,即全天都放鸽子则花费时间为0. # ...
- Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )
题目链接 题意 : 中文题.点链接 分析 : 有道题是问你不断求前缀和后的结果 Click here 这道题问的是逆过程 分析方法雷同.可参考 Click here ----------------- ...
- [BNUZOJ1261][ACM][2016北理校赛]方块消除(栈,字符串)
玩过方块消除游戏吗?现在规定当有两个或两个以上相邻且颜色相同的方块在一起的时候,它们就会产生消除反应.当存在多个消除反应同时产生时,最下的反应先执行.现在只给你其中一列,求最后剩下的方块结果. 输入要 ...
- HZNU第十二届校赛赛后补题
愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...
- 北邮14&18年软院机试【参考】答案
2014 Problem A. 奇偶求和 题目描述: 给定N个数,分别求出这N个数中奇数的和以及偶数的和. 输入格式 第一行为测试数据的组数T(1<=T<=50).请注意,任意两组测试数据 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
随机推荐
- 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring
[题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...
- MyBatis 系列五 之 延迟加载、一级缓存、二级缓存设置
MyBatis的延迟加载.一级缓存.二级缓存设置 首先我们必须分清延迟加载的适用对象 延迟加载 MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟加载规则推迟对关联对象的se ...
- 20、redis和memcached比较?
1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...
- Python eval 函数说明
eval(str [,globals [,locals ]]) -- 函数将字符串str当成有效Python表达式来求值,并返回计算结果. 例 : eval('3+4') ==> ...
- Java多线程学习(二)synchronized关键字(1)
转载请备注地址: https://blog.csdn.net/qq_34337272/article/details/79655194 Java多线程学习(二)将分为两篇文章介绍synchronize ...
- 【Windows使用笔记】Windows科研软件
1 Anaconda Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.主要是内置有jupyter notebook和jupyter ...
- kimbits_USACO
StringsobitsKim Schrijvers Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits ...
- "Flags mismatch irq" register interrupt handler error
Question : When you see the log "Flags mismatch irq ............", maybe you use the same ...
- Linux kernel suspend resume学习:2.6.35与3.0.35比较【转】
转自:http://blog.csdn.net/njuitjf/article/details/18317149 Linux kernel suspend resume学习:2.6.35与3.0.35 ...
- JAVA 线程状态及转化
线程状态图 说明:线程共包括以下5种状态.1. 新建状态(New) : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状 ...