【HDU 3590】 PP and QQ (博弈-Anti-SG游戏,SJ定理,树上删边游戏)

本文解析了一款名为PPandQQ的游戏策略问题,通过对抗型SG游戏理论来判断双方玩家在最优策略下谁将赢得游戏。文章介绍了游戏规则,并通过代码实现展示了如何计算最终赢家。

PP and QQ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 510    Accepted Submission(s): 256


Problem Description
PP and QQ were playing games at Christmas Eve. They drew some Christmas trees on a paper: 



Then they took turns to cut a branch of a tree, and removed the part of the tree which had already not connected with the root. A step shows as follows: 



PP always moved first. 
PP and QQ took turns (PP was always the first person to move), to cut an edge in the graph, and removed the part of the tree that no longer connected to the root. The person who cannot make a move won the game. 
Your job is to decide who will finally win the game if both of them use the best strategy. 

 

Input
The input file contains multiply test cases.
The first line of each test case is an integer N (N<100), which represents the number of sub-trees. The following lines show the structure of the trees. The first line of the description of a tree is the number of the nodes m (m<100). The nodes of a tree are numbered from 1 to m. Each of following lines contains 2 integers a and b representing an edge <a, b>. Node 1 is always the root. 

 

Output
For each test case, output the name of the winner.

 

Sample Input
2 2 1 2 2 1 2 1 2 1 2

 

Sample Output
PP QQ

 

Author
alpc27
 
 
【分析】
  首先是一个树上删边游戏。
  就是子树的sg+1的乘积啦。。不说了
  然后是Anti-SG游戏,有SJ定理,具体看jzh的论文吧。
  

 

  证明的话,只需说明:
  最后的状态符合这个定理。
  必胜态一定能到一个必败态。
  必败态到的都是必胜态 即可、
  这个自己YY瞎搞一下就能证明了。
 
  就是这样了。
 
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 #define Maxn 110
 8 
 9 struct node
10 {
11     int x,y,next;
12 }t[Maxn*2];
13 int first[Maxn],len;
14 
15 void ins(int x,int y)
16 {
17     t[++len].x=x;t[len].y=y;
18     t[len].next=first[x];first[x]=len;
19 }
20 
21 int dfs(int x,int f)
22 {
23     int ans=0;
24     for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
25     {
26         ans^=(dfs(t[i].y,t[i].x)+1);
27     }
28     return ans;
29 }
30 
31 int main()
32 {
33     int T;
34     while(scanf("%d",&T)!=EOF)
35     {
36         int ans=0;
37         bool ok=0;
38         while(T--)
39         {
40             int n;
41             scanf("%d",&n);
42             len=0;
43             for(int i=1;i<=n;i++) first[i]=0;
44             for(int i=1;i<n;i++)
45             {
46                 int x,y;
47                 scanf("%d%d",&x,&y);
48                 ins(x,y);ins(y,x);
49             }
50             int nw=dfs(1,0);
51             if(nw>1) ok=1;ans^=nw;
52         }
53         if((!ans&&!ok)||(ans&&ok)) printf("PP\n");
54         else printf("QQ\n");
55     }
56     return 0;
57 }
View Code

 

2017-04-27 17:11:55

转载于:https://www.cnblogs.com/Konjakmoyu/p/6775193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值