2019-2020 ICPC Asia Hong Kong Regional Contest B. Binary Tree(思维)
In computer science, a binary tree is a rooted tree in which each node has at most two children. In this problem, let's denote 𝑛n as the number of nodes, 𝑙l as the number of leaf nodes and ℎh as the height of the tree (a tree consisting of only a root node has a height of 00).
Alice and Bob are playing a game with a binary tree. In this game, Alice and Bob have a binary tree, in which node 11 is the root. They take turns to perform operations on the tree, and Alice always takes the first turn. In each operation, the player taking the turn must choose a node 𝑝p (any node including the root can be chosen), and remove the subtree rooted at 𝑝p from the tree. Obviously, the remaining graph, if not empty, is still a binary tree. Then they continue to play with the resulting tree. To make the game more interesting, there is a restriction on which nodes can be chosen as 𝑝p: the subtree rooted at 𝑝p (the subtree to be removed) must be a perfect full binary tree. Note that a perfect full binary tree is a binary tree in which all interior (non-leaf) nodes have two children and all leaf nodes have the same depth. It can be easily shown that in a perfect full binary tree, the equation 𝑙=2ℎl=2h holds, so does the equation 𝑛=2ℎ+1−1n=2h+1−1. In particular, a tree consisting of only a root node is also a perfect full binary tree. When a player is unable to perform a legal operation, the game ends and that player loses, which means the other player wins.
Three examples of perfect full binary trees.
Alice and Bob are both very smart and always play optimally. Can you determine who would win the game?
Input
The input contains multiple cases. The first line of the input contains a single positive integer 𝑇T, the number of cases.
For each case, the first line of the input contains a single integer 𝑛n (1≤𝑛≤50001≤n≤5000), the number of nodes in the binary tree. The following 𝑛−1n−1 lines each contains two integers 𝑥,𝑦x,y (1≤𝑥≤𝑛,1≤𝑦≤𝑛1≤x≤n,1≤y≤n), which denotes an edge between node 𝑥x and 𝑦y. It is guaranteed that the input graph is a binary tree rooted at node 11.
It's guaranteed that the sum of 𝑛n over all cases does not exceed 5000050000.
Output
For each case, print the string "Alice" in a single line if Alice would win the game, otherwise print the string "Bob".
Example
input
Copy
1
5
1 2
1 3
3 4
3 5
output
Copy
Alice
给一棵二叉树,两人轮流每次拿走一棵满二叉树(子树),其中一个节点也算子树,而且拿完后剩下的必须仍为一棵二叉树,问先手必胜还是必败。
注意到一棵满二叉树必须为奇数个点,这和拿走一个点的效果实际上是一样的,因此只需要看
这棵树有多少个点就行,n为奇数先手必胜。
(一开始sb了,想成了统计有多少个互不包含的满二叉树以及剩下的点的个数,理论上也能过但一直wa。。)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!