思路:无向图,走过的点不能在走。dfs搞定……
再就是后继中有必败点的为必胜点!
代码如下:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<vector> 6 #include<cstring> 7 using namespace std; 8 int ans; 9 vector<int>p[1001]; 10 bool vis[1001]; 11 int dfs(int n) 12 { 13 for(int i=0;i<p[n].size();i++){ 14 if(!vis[p[n][i]]){ 15 vis[p[n][i]]=1; 16 if(!dfs(p[n][i])){ 17 ans=p[n][i]; 18 return 1; 19 } 20 vis[p[n][i]]=0; 21 } 22 } 23 return 0; 24 } 25 int main() 26 { 27 int n,m,a,b; 28 while(scanf("%d%d",&n,&m)!=EOF){ 29 for(int i=0;i<n;i++) p[i].clear(); 30 memset(vis,0,sizeof(vis)); 31 for(int i=1;i<n;i++){ 32 scanf("%d%d",&a,&b); 33 p[a].push_back(b); 34 p[b].push_back(a); 35 } 36 vis[m]=1; 37 if(dfs(m)) printf("First player wins flying to airport %d ",ans); 38 else printf("First player loses "); 39 } 40 return 0; 41 }