PP and QQ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 315 Accepted Submission(s): 137
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.
![](https://i-blog.csdnimg.cn/blog_migrate/2cbd3e8493230b17b3a1a87dbf899aa4.jpeg)
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:
![](https://i-blog.csdnimg.cn/blog_migrate/1eca8f9c4558bdee782b9063dd67d2ca.jpeg)
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.
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和上一题是一样的,就是反着的, ANTI-SG: 先手必胜当且仅当:(1)游戏的SG函数不为0且游戏中某个单一游戏的SG函数大于1;(2)游戏的SG函数为0且游戏中没有单一游戏的SG函数大于1。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define M 100500
vector<int > vec[M];
int vis[M],dp[M];
int dfs(int x){
if(dp[x]!=-1)return dp[x];
vis[x]=1;
dp[x]=0;
for(int i=0;i<vec[x].size();i++){
if(!vis[vec[x][i]])
dp[x]^=dfs(vec[x][i])+1;
}
return dp[x];
}
int main()
{
int tcase,n,i,s,e;
while(scanf("%d",&tcase)!=EOF)
{
int cnt=0,ans=0,k;
while(tcase--){
scanf("%d",&n);
for(i=0;i<=n;i++)vec[i].clear();
memset(vis,0,sizeof(vis));
memset(dp,-1,sizeof(dp));
n--;
while(n--){
scanf("%d%d",&s,&e);
vec[s].push_back(e);
vec[e].push_back(s);
}
k=dfs(1);
ans^=k;
if(k>1)cnt++;
}
if(ans){
if(cnt)printf("PP\n");
else printf("QQ\n");
}
else {
if(cnt)printf("QQ\n");
else printf("PP\n");
}
}
return 0;
}