A tree game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 624 Accepted Submission(s): 335
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
题意:树上的删边游戏,删掉一条边后只保留与根节点向连的部分,最后无法删边的人输.
阅读论文以后知道了一个有趣的结论:这个游戏中某个节点的sg值等于他所有孩子节点sg值+1的异或和,
其中叶子节点的sg值等于0.
#include <bits/stdc++.h>
using namespace std;
#define maxn 211111
struct node {
int u, v, next;
}edge[maxn];
int head[maxn], cnt, n;
int sg[maxn];
void add_edge (int u, int v) {
edge[cnt].u = u, edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;
}
int dfs (int u, int father) {
int ans = 0;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v == father)
continue;
ans ^= (dfs (v, u)+1);
}
return ans;
}
int main () {
//freopen ("in.txt", "r", stdin);
int t;
scanf ("%d", &t);
while (t--) {
memset (head, -1, sizeof head);
cnt = 0;
scanf ("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf ("%d%d", &u, &v);
add_edge (u, v);
add_edge (v, u);
}
int ans = dfs (1, 0);
if (ans)
printf ("Alice\n");
else
printf ("Bob\n");
}
return 0;
}