hdu3590 PP and QQ-----删边游戏&&anti--sg

#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<vector>
using namespace std;
vector<int>v[110];
int dfs(int x,int pre)
{
    int ans=0;
    for(int i=0;i<v[x].size();i++)
    {
        if(v[x][i]!=pre)
        ans^=(1+dfs(v[x][i],x));
    }
    return ans;
}
int main()
{
    int n,m,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        int ans=0,cnt=0;
        while(n--)
        {
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
            v[i].clear();
            for(int i=1;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                v[a].push_back(b);
                v[b].push_back(a);
            }
            int s=dfs(1,-1);
            if(s>1) cnt++;
            ans^=s;
        }
        if(cnt>=1)
        {
            if(ans) puts("PP");
            else puts("QQ");
        }
        else
        {
            if(ans) puts("QQ");
            else puts("PP");
        }
    }
}

PP and QQ

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


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

Source

Recommend
zhouzeyong
多棵树,每次可以删掉和根相连的一部分树,谁不能操作谁就输了。
结合一下anti-sg,先手有必胜策略是:如果某个单一游戏的sg值大于1并且整个游戏的sg值不为0,或者游戏的sg值为0,且游戏中没有单一的游戏的sg值大于1.
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值