Zoj 3332 Strange Country II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.