poj - A funny game-2599

Description

There are several airports in one country, and there are flights between some of them. One can fly from any airport to any other, probably with some changes. For any pair of airports there exists only one sequence of flights that connects them.
Two terrorists play a game. They make moves in turn. Each move consists of the following operations. A player mines an airport, chooses a flight and flies away together with his colleague. After the take-off he actuates a radio-controlled fuse. As a result the airport that the terrorists have just left is destroyed, and all the flights to and from this airport are no longer possible. After the aircraft lands the other player makes his move, and so forth. One loses if one cannot make a move.
Given an initial list of flights and the number of an airport where the terrorists are at the start of the game, write a program which would determine who wins if the terrorists play a perfect game (each chooses the best move).

Input

The first line of an input contains two integers: N and K separated with a space. Here N is the number of airports (N <= 1000) and K is the number of an airport, which is the starting point of the game (1 <= K<= N). The next n-1 lines of the file contain pairs of integers separated with a space. These integers are numbers of airports, connected with a flight (all the flights are symmetric and are mentioned only once). There are at most 20 flights to each airport. Input data are always correct.

Output

If the player who starts the game wins, the program should write: “First player wins flying to airport L” where L is the number of an airport to which the players should fly first. If there are more than one such airports, the program should find one of them that has the minimal number. Otherwise the program should write “First player loses”.

Sample Input

4 3
3 2
3 1
1 4

Sample Output

First player wins flying to airport 2

Source

Ural State University collegiate programming contest 2000

题解

题意
  • 给你一个N个结点N-1条边的无向图,飞机从结点K出发,两个人轮流坐飞机移动,走过的结点机场不能再走。无法移向下一个结点的人输,问第一个玩家能否获胜?如
    果能获胜,给出第一个玩家的第一步。
  • 如下图,K=3,如果第一个玩家飞往2,第二个玩家不能再走,第二个玩家输。如果飞往1,第二个玩家只能飞往4,第一个玩家会输,但是两个玩家足够聪明,玩家一肯定选择飞往2,然后输出答案2即可。
    这里写图片描述
附C++代码
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=1010;
bool Map[maxn][maxn],vis[maxn];
int N,L;
bool Win(int k)//dfs
{
    vis[k]=true;
    for(int i=1;i<=N;++i){
        if(Map[k][i] && !vis[i]){
            if(!Win(i)){
                L=i;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int K;
    while(~scanf("%d%d",&N,&K))
    {
        memset(Map,0,sizeof(Map));
        memset(vis,0,sizeof(vis));
        int s,e;
        for(int i=1;i<=N-1;++i){
            scanf("%d%d",&s,&e);
            Map[s][e]=Map[e][s]=1;//双向连通
        }
        if(Win(K)) printf("First player wins flying to airport %d\n",L);
        else printf("First player loses\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值