当前位置: 首页 > news >正文

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\n");
53         else
54             printf("Bob\n");
55     }
56 }

 

转载于:https://www.cnblogs.com/quintessence/p/6708730.html

相关文章:

  • hibernate之复合主键作为外键的相关配置
  • js中match函数方法
  • 51NOD 1237 最大公约数之和 V3 [杜教筛]
  • 20169219 2016-2017-2 《移动平台开发》第七周作业
  • Verilog基础知识0(`define、parameter、localparam三者的区别及举例)
  • redis安装配置
  • U-Mail邮件中继针对性横扫邮件通关六大阻力
  • 博客、文章索引。
  • 洛谷P1508 Likecloud-吃、吃、吃 [2017年4月计划 动态规划10]
  • sublime text3及插件安装过程
  • U872-结算成本处理步骤及索引处理
  • Python 3.5 in win10 pip install Orange3
  • 记一次前端工程构建
  • Linux top、VIRT、RES、SHR、SWAP(S)、DATA Memory Parameters Detailed
  • Sping Boot + Spring Security + Mybaits + Logback +JWT验证 项目开发框架搭建
  • 【刷算法】从上往下打印二叉树
  • 【刷算法】求1+2+3+...+n
  • 2017 前端面试准备 - 收藏集 - 掘金
  • 2017-08-04 前端日报
  • C++类中的特殊成员函数
  • JDK 6和JDK 7中的substring()方法
  • jquery cookie
  • js 实现textarea输入字数提示
  • js操作时间(持续更新)
  • Just for fun——迅速写完快速排序
  • laravel with 查询列表限制条数
  • spring boot 整合mybatis 无法输出sql的问题
  • 分类模型——Logistics Regression
  • 码农张的Bug人生 - 初来乍到
  • 如何在 Tornado 中实现 Middleware
  • 深度学习中的信息论知识详解
  • 微信小程序:实现悬浮返回和分享按钮
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • ​【已解决】npm install​卡主不动的情况
  • ​一些不规范的GTID使用场景
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (42)STM32——LCD显示屏实验笔记
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (四)鸿鹄云架构一服务注册中心
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (转)mysql使用Navicat 导出和导入数据库
  • (转载)CentOS查看系统信息|CentOS查看命令
  • (转载)PyTorch代码规范最佳实践和样式指南
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .NET MVC 验证码
  • .NET分布式缓存Memcached从入门到实战
  • .NET开源项目介绍及资源推荐:数据持久层 (微软MVP写作)
  • @select 怎么写存储过程_你知道select语句和update语句分别是怎么执行的吗?
  • [ C++ ] template 模板进阶 (特化,分离编译)