hdu3590(无向图删边)

PP and QQ

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


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
 

其中树的删边游戏叶子节点的SG值为0;中间节点的SG值为它的所有子节点的SG值加1 后的异或和。

ANTI-SG:先手必胜当且仅当:(1)游戏的SG函数不为0且游戏中某个单一游戏的SG函数大于1;(2)游戏的SG函数为0且游戏中没有单一游戏的SG函数大于1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define N 10005
using namespace std;
vector<int>v[100005];
int get_sg(int u,int pre)
{
int ret=0,i;
for(i=0; i<v[u].size(); i++)
if(v[u][i]!=pre)
ret^=(1+get_sg(v[u][i],u));
return ret;
}
int main()
{
int t,n,i;
int x,y;
while(scanf("%d",&t)!=EOF)
{
int ret=0,cnt=0;
while(t--)
{
scanf("%d",&n);
for(i=1; i<=n; i++)
v[i].clear();
for(i=1; i<n; i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int s=get_sg(1,-1);
if(s>1)
cnt++;
ret^=s;
}
if(cnt==0)
{
if(ret)
puts("QQ");
else
puts("PP");
}
else
{
if(ret)
puts("PP");
else
puts("QQ");
}
}
return 0;
}

原文地址:https://www.cnblogs.com/lxm940130740/p/3282272.html