hdu 3094 A tree game 博弈论

思路:

叶子节点的SG值为0;中间节点的SG值为它的所有子节点的SG值加1
后的异或和。

详见贾志豪神牛的论文:组合游戏略述 ——浅谈SG游戏的若干拓展及变形

代码如下:

 1 #include<cstdio>
 2 #include<vector>
 3 using namespace std;
 4 vector<int>p[100002];
 5 int get_sg(int n,int u)
 6 {
 7     int ans=0;
 8     for(int i=0;i<p[n].size();i++){
 9         if(p[n][i]!=u) ans^=(1+get_sg(p[n][i],n));
10     }
11     return ans;
12 }
13 int main()
14 {
15     int i,t,n,u,v;
16     scanf("%d",&t);
17     while(t--){
18         scanf("%d",&n);
19         for(i=1;i<=n;i++) p[i].clear();
20         for(i=1;i<n;i++){
21             scanf("%d%d",&u,&v);
22             p[u].push_back(v);
23             p[v].push_back(u);
24         }
25         puts(get_sg(1,-1)?"Alice":"Bob");
26     }
27     return 0;
28 }
View Code

原文地址:https://www.cnblogs.com/xin-hua/p/3301404.html