1045. Funny Game
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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 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 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.
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 several such airports, the program should find one of them that has the minimal number. Otherwise the program should write “First player loses”.
Sample
input | output |
---|---|
4 3 3 2 3 1 1 4 | First player wins flying to airport 2 |
Problem Author: Dmitry Filimonenkov
Problem Source: Ural State University collegiate programming contest (25.03.2000)
Problem Source: Ural State University collegiate programming contest (25.03.2000)
/**
*
* 其中有N个机场和N-1条航线,所有机场可以互达。一开始两个人在某一个机场,两个人轮流决定下一步的目的地机场,他们飞离机场后就会把那个机场炸掉。如果轮到谁时不能走了,他就算输了。
* dfs所有的路径然后找出节点的sg值,然后按照最优策略找出结果。
*
*/
#include <iostream>
#include <vector>
using namespace std;
vector <int> g[1005];
bool dis[1005],sg[1005];
int i,n,k,x,y,lea;
void dfs(int kk)
{
dis[kk]=true;
if(g[kk].size()==0)
return;
for(int i=0; i<g[kk].size(); i++)
{
if(!dis[g[kk][i]])
{
dfs(g[kk][i]);
if(!sg[g[kk][i]])
{
sg[kk]=true;
if(kk==k&&lea>g[kk][i])
lea=g[kk][i];
}
}
}
return;
}
int main()
{
cin>>n>>k;
for(i=0; i<n-1; i++)
{
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
lea=10005;
dfs(k);
if(sg[k])
cout<<"First player wins flying to airport "<<lea<<endl;
else
cout<<"First player loses"<<endl;
return 0;
}