A person arrives in a strange city, the characteristic of this city is: every two cities has the plane to arrive directly, is not two-way ,
Give the number of cities N, and N * (n-1)/2 cities X to the City y Road, (means X to Y has a plane), ask you whether you can pass every city and only once
If possible, the output passes through the path of the city, otherwise the output impossible.
Solution: Traverse each point as the starting point for DFS, and if it is possible to traverse all the points, the output path. But there were some mistakes.
has already been raised in the code. After Dfs (x,cur+1), do not forget to judge the flag return, otherwise vis[i] is set to 0, will flush out the original value of ans.
DFS (i) returns the last statement of the previous layer called DFS, which is noted.
#include <iostream>#include<string>#include<cstring>#include<algorithm>using namespacestd;intmap[102][102];intvis[102];intans[102];intFlag;intt,n,a,b,cur;voidDfsintXintcur) { if(cur==N) {flag=true; return; } for(intI=1; i<=n;i++) { if(map[x][i]&&!Vis[i]) {Ans[cur]=i; Vis[i]=1; DFS (I,cur+1); if(flag)//The answer is right here return, otherwise the ANS array (vis[i] is changed to 0) return ; Vis[i]=0; } }}intMain () {CIN>>T; while(t--) {cin>>N; Flag=false; memset (Map,0,sizeof(map)); for(intI=1; i<=n* (n1)/2; i++) {cin>>a>>b; MAP[A][B]=1; } for(intI=1; i<=n;i++) {memset (Vis,0,sizeof(VIS)); Vis[i]=1; ans[0]=i; DFS (i,1); if(flag) {intfirst=0; for(intj=0; j<n;j++) { if(first++) cout<<" "; cout<<Ans[j]; } cout<<Endl; Break; } } if(!flag) {cout<<"Impossible"<<Endl; } } return 0;}
Zoj 3332 Strange Country II