1282.Game Tree

1908 ワード

http://acm.timus.ru/problem.aspx?space=1&num=1282
簡単なゲームは題意の理解に注意します。
コード:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<vector>

#include<queue>

#include<map>

#include<stack>

#include<algorithm>

#include<cmath>



using namespace std;

//#pragma comment(linker,"/STACK:1000000000,1000000000")



#define LL long long



const int INF=0x3f3f3f3f;

const int N=1005;

int win[N];

int L[N];

int head[N];

struct node

{

    int j,next;

}side[N];

int I;

void Add(int i,int j)

{

    side[I].j=j;

    side[I].next=head[i];

    head[i]=I++;

}

int dfs(int x,int w)

{

    if(L[x]!=-2)

    return L[x];

    int k=(w==1)?-1:1;

    for(int t=head[x];t!=-1;t=side[t].next)

    {

        int l=side[t].j;

        if(w==1)

        k=max(k,dfs(l,-w));

        else

        k=min(k,dfs(l,-w));

    }

    return k;

}

int main()

{

    //freopen("data.txt","r",stdin);

    int n;

    while(scanf("%d",&n)!=EOF)

    {

        I=0;

        memset(head,-1,sizeof(head));

        for(int i=1;i<=n;++i)

        {L[i]=-2;win[i]=-2;}

        for(int i=2;i<=n;++i)

        {

            char c;

            int f,k;

            cin>>c>>f;//cout<<c<<" "<<f<<endl;

            Add(f,i);

            if(c=='L')

            {

                cin>>k;

                L[i]=k;

            }

        }

        int tmp=dfs(1,1);

        if(tmp==1)

        cout<<"+1"<<endl;

        else if(tmp==-1)

        cout<<"-1"<<endl;

        else

        cout<<"0"<<endl;

    }

    return 0;

}