http://acm.timus.ru/problem.aspx?space=1&num=1045

简单博弈

代码:

#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 head[N],I;
struct node
{
    int j,next;
}side[N*2];
void Add(int i,int j)
{
    side[I].j=j;
    side[I].next=head[i];
    head[i]=I++;
}
int dfs(int x,int pre)
{
    int k=0;
    for(int t=head[x];t!=-1;t=side[t].next)
    {
        int l=side[t].j;
        if(l==pre)
        continue;
        if(dfs(l,x)==0)
        {
            if(k==0||l<k)
            k=l;
        }
    }
    return k;
}
int main()
{
    //freopen("data.txt","r",stdin);
    int n,s;
    while(cin>>n>>s)
    {
       memset(head,-1,sizeof(head));
       I=0;
       for(int i=1;i<n;++i)
       {
           int l,r;
           cin>>l>>r;
           Add(l,r);
           Add(r,l);
       }
       int k=dfs(s,-1);;
       if(k)
       cout<<"First player wins flying to airport "<<k<<endl;
       else
       cout<<"First player loses"<<endl;
    }
    return 0;
}

 

posted on 2012-10-18 20:25  夜->  阅读(191)  评论(0编辑  收藏  举报