http://poj.org/problem?id=2599
#include <iostream>
#include <cstring>
using namespace std;
int N,K;
bool map[1010][1010];
bool visited[1010];
bool dfs(int n,int step)
{
bool z = false;
visited[n] = true;
bool HasPath = false;
for(int i = 1;i <= N;i++)
{
if(!visited[i] && map[n][i])
{
HasPath = true;
z = dfs(i,step+1);
if(step % 2 == 0)
{
if(z)
return true;
}
else
{
if(!z)
return false;
}
}
}
if(!HasPath)
{
if(step % 2 == 0)
return false;
else
return true;
}
return z;
}
int main()
{
while(cin>>N>>K)
{
int x,y;
for(int z = 0;z < N - 1;z++)
{
cin>>x>>y;
map[x][y] = map[y][x] = true;
}
bool Yes = false;
int i;
for(i = 1;i <= N;i++)
{
memset(visited,0,sizeof(visited));
if(map[K][i])
{
visited[K] = true;
bool n = dfs(i,1);
if(n)
{
Yes = true;
break;
}
}
}
if(Yes)
cout<<"First player wins flying to airport "<<i<<endl;
else
cout<<"First player loses"<<endl;
for(int i = 0;i < 1000;i++)
{
for(int j = 0;j < 1000;j++)
map[i][j] = false;
}
}
return 0;
}
/*
A funny game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1281 Accepted: 510
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
*/