hdu3590 PP and QQ

67 篇文章 1 订阅

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.

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
和上一题是一样的,就是反着的, 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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值