2020 GDUT Rating Contest IV (Div. 2) E. Binary Tree

来源 codeforces 2020 GDUT Rating Contest IV (Div. 2) CF链接

题目:
outputstandard output
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 0).
在这里插入图片描述
Alice and Bob are playing a game with a binary tree. In this game, Alice and Bob have a binary tree, in which node 1 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 l=2h holds, so does the equation n=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≤n≤5000), the number of nodes in the binary tree. The following n−1 lines each contains two integers x,y (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 1.

It’s guaranteed that the sum of n over all cases does not exceed 50000.

Output
For each case, print the string “Alice” in a single line if Alice would win the game, otherwise print the string “Bob”.

Example
inputCopy
1
5
1 2
1 3
3 4
3 5
outputCopy
Alice
Note
In the sample case, Alice removes the subtree rooted at node 3 in the first turn. Then Bob can only choose p=2, which leaves Alice with only the root node 1. Because a tree consisting of only a root node is a perfect full binary tree, Alice can remove the only remaining node and win the game.

题意:一棵二叉树,每次能拿走一个完整的子树(非叶子结点必须有左右儿子,叶子没有儿子),最后拿的赢。

思路:很容易被骗去建树来做,然而其实是一道思维题。每次只能拿走一个完整的树,即每次一定拿走奇数个结点。
考虑原本就有奇数个点的情况,有两种:树本来就是一个完整的二叉树,这时候第一个人就直接全拿走了。第二种就是改树不是完整二叉树,故一次不能拿光,拿走奇数个之后变为剩下偶数个结点,偶数个结点不可能被拿光,故每次第一个人都是奇数个可以拿,第二个都是偶数个可以拿,故第一个必胜。
偶数点情况:第一个人拿了奇数个点后第二个必是一个奇数个点的树可以拿,如果不能拿完就剩一个偶数个点的树给第一个人那,与情况1同理,第二人必胜。
故原先奇数个点的时候第一人胜利,否则第二人胜利

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

int main()
{
	int n,t,x,y;
	cin>>t;
	while (t--)
	{
		cin>>n;
		if (n%2==0) printf("Bob\n");
		else printf("Alice\n");
		while (n-->1)
		cin>>x>>y;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值