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.
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.
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值加一的异或,叶子结点的sg为零。
主要来记录一发。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include <cmath>
#include<iostream>
#include<algorithm>
#include <vector>
#include <bitset>
#define maxn 100005
#define ll long long
using namespace std;
vector<int> g[maxn];
int sg[maxn],vis[maxn];
int n;
int getsg(int k)
{
if(sg[k]!=-1) return sg[k];
sg[k]=0;
vis[k]=1;
for(int i=0;i<g[k].size();i++)
{
if(!vis[g[k][i]]) //这里只是将他们的父节点标记了,这里只要不是父节点的就可以遍历
sg[k]^=(getsg(g[k][i])+1);
}
vis[k]=0; //子节点遍历完毕,对于自己这个父节点的标记需要取消以免影响其他结果
return sg[k];
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
memset(vis,0,sizeof(vis));
memset(sg,-1,sizeof(sg));
for(int i=0;i<=n;i++)
g[i].clear();
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
if(getsg(1)) printf("Alice\n");
else printf("Bob\n");
}
return 0;
}