Problem Description
Alice and Bob want to play an interesting game on a tree.
Given is a tree on N vertices, The vertices are numbered from 1 to N. vertex 1 represents the root. There are N-1 edges. Players alternate in making moves, Alice moves first. A move consists of two steps. In the first step the player selects an edge and removes it from the tree. In the second step he/she removes all the edges that are no longer connected to the root. The player who has no edge to remove loses.
You may assume that both Alice and Bob play optimally.
Given is a tree on N vertices, The vertices are numbered from 1 to N. vertex 1 represents the root. There are N-1 edges. Players alternate in making moves, Alice moves first. A move consists of two steps. In the first step the player selects an edge and removes it from the tree. In the second step he/she removes all the edges that are no longer connected to the root. The player who has no edge to remove loses.
You may assume that both Alice and Bob play optimally.
Input
The first line of the input file contains an integer T (T<=100) specifying the number of test cases.
Each test case begins with a line containing an integer N (1<=N<=10^5), the number of vertices,The following N-1 lines each contain two integers I , J, which means I is connected with J. You can assume that there are no loops in the tree.
Each test case begins with a line containing an integer N (1<=N<=10^5), the number of vertices,The following N-1 lines each contain two integers I , J, which means I is connected with J. You can assume that there are no loops in the tree.
Output
For each case, output a single line containing the name of the player who will win the game.
Sample Input
3 3 1 2 2 3 3 1 2 1 3 10 6 2 4 3 8 4 9 5 8 6 2 7 5 8 1 9 6 10
Sample Output
Alice Bob Alice
Source
Recommend
lcy
本题是经典的树上删边问题,关于树上删边和图上删边的问题可以看这一篇博客:我是“这篇博客”
Code:
#include<cstdio> #include<cstring> using namespace std; const int maxn = 1e5 + 5; struct edge {int v, next;}e[maxn << 1]; int head[maxn], sg[maxn], idx[maxn], cnt; int n, u, v, t; void adde(const int &u, const int &v) { e[++cnt] = (edge) {v, head[u]}; head[u] = cnt; } void dfs(int u, int p) { sg[u] = 0; for(register int i = head[u]; i; i = e[i].next) { int v = e[i].v; if(v == p) continue; dfs(v, u), sg[u] ^= sg[v] + 1; } } int main() { scanf("%d", &t); while(t--) { cnt = 0, memset(head, 0, sizeof(head)); scanf("%d", &n); for(register int i = 1; i < n; ++i) scanf("%d%d", &u, &v), adde(u, v), adde(v, u); dfs(1, 0); printf("%s\n", sg[1] ? "Alice" : "Bob"); } return 0; }
没有帐号? 立即注册