A funny game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1654 Accepted: 692
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
用vector竟然沒寫出來~哭~
[cpp]
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
using namespace std;
bool mat[1010][1010];
bool flag[1010];
int n;
int dfs(int x)
{
flag[x]=false;
for(int i=1;i<=n;i++)
{
if(mat[x][i]&&flag[i])
{
flag[i]=false;
if(dfs(i)==0)
{
flag[i]=true;
return i;
}
flag[i]=true;
}
}
return 0;
}
int main()
{
int k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(mat,false,sizeof(mat));
memset(flag,true,sizeof(flag));
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mat[a][b]=mat[b][a]=true;
}
int d=dfs(k);
if(d==0) puts("First player loses");
else
printf("First player wins flying to airport %d\n",d);
}
}
錯誤代碼:先留著,有時間找找錯誤
[cpp]
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>v[1010];
int g[22];
int dfs(int x)
{
// cout<<"bug"<<endl;
for(int i=0;i<v[x].size();i++)
{
int count=0;
int f=v[x][i];
for(int j=0;j<v[x].size();j++)
{
int q=v[x][j];
g[count++]=q;
for(int k=0;k<v[q].size();k++)
{
if(v[q][k]==x)
{
v[q].erase(v[q].begin()+k);
break;
}
}
v[x].erase(v[x].begin()+j);
}
if(dfs(f)==0)
{
for(int j=0;j<count;j++)
{
v[x].push_back(g[j]);
v[g[j]].push_back(x);
}
return 1;
}
for(int j=0;j<count;j++)
{
v[x].push_back(g[j]);
v[g[j]].push_back(x);
}
}
return 0;
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=0;i<=n;i++)
v[i].clear();
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
sort(v[k].begin(),v[k].end());
bool flag=true;
for(int i=0;i<v[k].size();i++)
{
if(dfs(v[k][i])==0)
{
flag=false;
printf("First player wins flying to airport %d\n",v[k][i]);
break;
}
}
if(flag) puts("First player loses");
}
}