2039 horse riding fence, 2039 horse riding fence

Source: Internet
Author: User

2039 horse riding fence, 2039 horse riding fence
DescriptionDescription

Farmer John has a lot of fences to repair each year. He always rode his horse across every fence and repaired its broken location.

John is just as lazy as other farmers. He hates horse riding, so he never goes through a fence twice. You must compile a program, read the description of the fence network, and calculate a fence repair path so that each Fence passes exactly once. John can start riding from any vertex (that is, the intersection of two fences) and end at any vertex.

Each fence connects two vertices with a 1-500 label (although some farms do not have 500 vertices ). A vertex can be connected to any number of fences (> = 1. There may be multiple fences between the two vertices. All the fences are connected (that is, you can reach all the fences from any one ).

Your program must output the path of the horse riding (represented by the vertex number that passes through the road in turn ). If we regard the output path as a 500 hexadecimal number, when there are multiple groups of solutions, output is the smallest of the 500 hexadecimal notation (that is, the first number of outputs is smaller. If there are multiple groups of solutions, the second number is smaller, and so on ).

Input data must have at least one solution.

Input description Input Description

Row 1st: an integer F (1 <= F <= 1024), indicating the number of fences.

Rows 2nd to F + 1: each row has two integers, I. j (1 <= I, j <= 500) indicates that the barrier connects the vertex I and j.

Output description Output Description

The output should contain rows F + 1 with an integer in each line, indicating the vertex number passing through the path in turn. Note that data may have multiple groups of solutions, but only the group of solutions required by the above questions is correct.

Sample Input Sample Input
91 22 33 44 24 52 55 65 74 6
Sample output Sample Output
1234254657
Data range and prompt Data Size & Hint

See description

 

Ideas:

1. upgraded version with one stroke

2. You must start with the minimum value of an edge when searching.

3. The range must be the maximum number instead of the n value.

4. The data has the same situation, so you cannot simply set map to 1.

5. output format! Output Format! Output Format! Output Format! Output Format! Output Format! Output Format! Output Format! Output Format! Output Format! Output Format!

 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 int map[2000][2000]; 6 int tj[10001]; 7 int vis[2000]; 8 int n,m; 9 int ans[2000];10 int now=1;11 int flag=1;12 int maxn=0,maxnnow=0;13 void dfs(int i) {14     //cout<<p<<" ";15     for(int j=1; j<=maxn; j++) 16     {17         if(map[i][j]>0) 18         {19             map[j][i]--;20             map[i][j]--;21             dfs(j);22         }23     }24     ans[now]=i;25     now++;26 }27 int main() 28 {29     30     scanf("%d",&n);31     m=n;32     for(int i=1; i<=m; i++) 33     {34         int x,y;35         scanf("%d%d",&x,&y);36         map[y][x]++;37         map[x][y]++;38         tj[x]++;39         tj[y]++;40         maxn=max(maxn,max(x,y));41     }42     flag=0;43     for(int i=1; i<=maxn; i++) 44         if(tj[i]%2==1) 45         {46             flag=i;47             break;48         }49     if(flag==0)50     {51         for(int i=1;i<=maxn;i++)52         {53             if(tj[i]>0)54             flag=i;55             break;56         }57     }58     dfs(flag);59     for(int i=now-1; i>=1; i--)60     cout<<ans[i]<<endl;61     return 0;62 }

 

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.