HDU - 3590 PP and QQ 樹的刪邊博弈+anti_sg

題目:n棵樹的刪邊遊戲,每次可以選擇一棵樹刪邊,與根節點不相連的邊和節點會移走,進行最後一步操作的人輸。

思路:

樹的刪邊遊戲:葉子節點的SG值爲0;中間節點的SG值爲它的所有子節點的SG值加1 後的異或和。
ANTI-SG:先手必勝當且僅當:(1)遊戲的SG函數不爲0且遊戲中某個單一遊戲的SG函數大於1;(2)遊戲的SG函數爲0且遊戲中沒有單一遊戲的SG函數大於1。

代碼:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
//0x3f3f3f3f

const int maxn=1e2+5;
vector<int> G[maxn];
int sg[maxn];
int get_sg(int u,int fa){
    if(sg[u]!=-1)
        return sg[u];
    sg[u]=0;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==fa) continue;
        get_sg(v,u);
        sg[u]=(sg[u]^(1+sg[v]));
    }
    return sg[u];
}
int main(){

    int n,m,u,v;
    while(~scanf("%d",&n)){
        int ans=0,flag=0;
        while(n--){
            scanf("%d",&m);
            for(int i=1;i<=m;i++){
                G[i].clear();
                sg[i]=-1;
            }
            for(int i=1;i<m;i++){
                scanf("%d%d",&u,&v);
                G[u].push_back(v);
                G[v].push_back(u);
            }
            get_sg(1,0);
            ans=(ans^sg[1]);
            if(sg[1]>1) flag=1;
        }
        if(ans>0&&flag) printf("PP\n");
        else if(ans==0&&flag==0) printf("PP\n");
        else printf("QQ\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章