HDU 3094 A tree game

时间:2022-02-05 04:48:11
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
每次从树上删除一条边,之后删除所有与根不相连的边
首先定义竹子:
HDU 3094 A tree game

根据克朗原理,一个分支可以转化为以这个点为根,以各个分支边数量的异或和为长度的竹子

实际上等价于

$SG[x]=(SG[v1]+1)xor(SG[v2]+1)xor(SG[v3]+1)...$

加1是因为要考虑x到v的边

然后递归处理SG函数就行了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Node
{
int next,to;
}edge[];
int head[],num,SG[],n;
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
void dfs(int x,int pa)
{int i;
SG[x]=;
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
dfs(v,x);
SG[x]^=(SG[v]+);
}
}
int main()
{int T,i,u,v;
cin>>T;
while (T--)
{
memset(head,,sizeof(head));
num=;
scanf("%d",&n);
for (i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs(,);
if (SG[])
printf("Alice\n");
else printf("Bob\n");
}
}

从某一棵树上删除一条边,

同时删去所有在删除边后不再与根相连的部分。

双方轮流进行,

无法再进行删除者判定为失败