UVa 10054:the Necklace, Euler loop + print path

Source: Internet
Author: User
Tags printf

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_ problem&problem=995

Type of topic: Euler loop

Topic:

My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a common color at their meeting point. The figure below shows a segment of the necklace:

But, alas! One day, the necklace is torn and the beads were all scattered over the floor. My sister did her best to recollect all beads from the floor, but she isn't sure whether she was able to collect all of them. Now, she has come to me for help. She wants to know whether it are possible to make a necklace using all the beads she has in the same way her original ACE is made and if so, the bids must is put.

Please help me write a program to solve the problem.

Translation of the title to the effect:

My sister has a string of necklaces made up of various colours. The joint of two consecutive beads in the necklace shares the same color. As pictured above, the first bead is green+red, then the beads must begin with red, as shown in the Red+white.

Oh! Oh, my God! One day, the necklace was ripped off and the beads fell. My sister tried her best to pick up the beads, but she was not sure if they had all been picked up. Now she asked me to help.

She wondered if it would be possible to connect the beads all together, in the same way as the original necklace.

Please help me to write a program to solve this problem.

Sample input:

2
5
1 2 2 3 3 4 4 5 5 6 5 2 1 2 2 3 4 3 1 2-
4

Sample output:

Case #1
Some beads could lost case
 
#2
2 1
1 3
3
4 4 2 2 2

Ideas and Summary:

This is the Euler loop + print path,

And the UVA 10129-play on Words, the Euler road is a very similar question,

The difference is that the problem is a direction diagram, and this problem is a map, the problem is to find Euler road, and this question is the European pull circuit.

The difference between Euler's and Euler's loops is that the Euclidean loop is going to start at a certain point, and finally back to this point, to form a loop. And Euler's path is from a point of view to the end of another point.

The method of printing the Euler loop, Liu Rugia "Introduction to the algorithm" P112 in detail:

The following recursive functions apply simultaneously to Euler's roads and loops. If you need to print an Euler path, the parameter must be the starting point for the path when the main program is called. In addition, the order in which you print is ordered so that you really use this

Code, you should replace the printf statement with a push statement and push the edges into a stack.

void Euler (int u) {for  
    (int v=0; v<maxn; ++v) if (G[u][v]) {  
        vis[u][v] = vis[v][u] = 1;  
        Euler (v);  
        printf ("%d%d\n", u,v);  
    }  

On the code used in the direction of the graph, if converted to the graph, the vis[u][v] = vis[v][u] = 1 to vis[u][v]= 1 can be.

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.