GDUT_排位赛题解报告_第4场_E. 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 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.

题意就是给出一颗二叉树,然后两个人轮流移除完美二叉树,直到某一方拿走最后的点。

思路就是:每次拿走的完美二叉树,你会发现,点的数量是奇数,也就是两人轮流拿去一个奇数,这样的博弈,从奇偶性上面来直接判断谁胜谁负。

完整代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜头文件
using namespace std;
const int INF = 0x3f3f3f3f;
//1.06e9大小
const int mod = 1e9+7;
typedef unsigned long long ULL;
typedef long long LL;
//鬼畜define
int main()
{
	int t;
	scanf("%d",&t);
	int n;
	while(t--)
	{
		scanf("%d",&n);
		int from,to;
		for(int time=0;time<n-1;time++){scanf("%d %d",&from,&to);}
		if(n%2==0)printf("Bob\n");
		else printf("Alice\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值