E. Binary Tree

GDUT 2020寒假训练 排位赛四 E

原题链接

题目

在这里插入图片描述
在这里插入图片描述
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.

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”.

样例

input

1
5
1 2
1 3
3 4
3 5

output
Alice

题目大意

有一个二叉树,Alice和Bob对这颗树轮流进行操作(Alice先操作),每一次操作可以删除这个树中的一个满二叉树(单独叶子节点也算),最后谁没有可以删的余地,对方就赢了,输出赢的人的名字。

思路

找规律
通过题意,我们可以知道,删除操作一共有两种,第一种是仅删除当前树的一个叶子节点,第二种是删除一个小的满二叉树(至少三个点的那种),我们就一个最简单的只有三个点的满二叉树来说,Alice先来。第一种情况,从叶子节点来删除:Alice删除了左节点,Bob删除了右节点,Alice删除了根节点,Bob输了。第二种情况,Alice删除了整个满二叉树,Bob输了。
也就是在只有三个点的满二叉树中,先删除叶子节点和删除整个二叉树结果是一样的。
那么对于一棵二叉树,他的组成肯定是由单独的叶子节点和三个点的满二叉树组成,既然先按点删和按三个点的数删没有区别,我们不妨让每一个人都按照点来删除,这样的话,无论这棵树长成什么样子。最后的结果只与这棵树有多少个点相关,奇数个点Alice获胜;偶数个点Bob获胜。

代码

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		if(n%2==0)
		{
			cout<<"Bob"<<endl;
		}
		else
		{
			cout<<"Alice"<<endl;
		}
		int x,y;
		for(int i=1;i<=n-1;i++)
		{
			cin>>x>>y;
		}
	}
	
	
	return 0;
}

发布了33 篇原创文章 · 获赞 1 · 访问量 679

猜你喜欢

转载自blog.csdn.net/xcy2001/article/details/104636039