Game on Tree

D - Game on Tree


Time limit : 2sec / Memory limit : 256MB

Score : 1100 points

Problem Statement

There is a tree with N vertices numbered 1,2,…,N. The edges of the tree are denoted by (xi,yi).

On this tree, Alice and Bob play a game against each other. Starting from Alice, they alternately perform the following operation:

  • Select an existing edge and remove it from the tree, disconnecting it into two separate connected components. Then, remove the component that does not contain Vertex 1.

A player loses the game when he/she is unable to perform the operation. Determine the winner of the game assuming that both players play optimally.

Constraints

  • 2≤N≤100000
  • 1≤xi,yiN
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x1 y1
x2 y2
:
xN−1 yN−1

Output

Print Alice if Alice wins; print Bob if Bob wins.


Sample Input 1

Copy
5
1 2
2 3
2 4
4 5

Sample Output 1

Copy
Alice

If Alice first removes the edge connecting Vertices 1 and 2, the tree becomes a single vertex tree containing only Vertex 1. Since there is no edge anymore, Bob cannot perform the operation and Alice wins.


Sample Input 2

Copy
5
1 2
2 3
1 4
4 5

Sample Output 2

Copy
Bob

Sample Input 3

Copy
6
1 2
2 4
5 1
6 3
3 2

Sample Output 3

Copy
Alice

Sample Input 4

Copy
7
1 2
3 7
4 6
2 3
2 4
1 5

Sample Output 4

Copy
Bob

在树上的博弈,从0,1开始看看这棵树的异或和

#include <bits/stdc++.h>
using namespace std;
const int N=100010;
vector<int>vec[N];
int dfs(int u,int fa) {
    int re=0;
    for(int i=0; i<vec[u].size(); i++) {
        if(vec[u][i]!=fa) {
            re^=1+dfs(vec[u][i],u);
        }
    }
    return re;
}
int main() {
        int n;
        scanf("%d",&n);
        for(int i=1; i<n; i++) {
            int a,b;
            scanf("%d%d",&a,&b);
            vec[a].push_back(b);
            vec[b].push_back(a);
        }
    printf(dfs(1,0)?"Alice\n":"Bob\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/7272165.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值