Ultraviolet A 10054-The neck133, Euler Loop + print path

Source: Internet
Author: User
10054-
Neck.pdf
12644 20.68% 2011 65.39%

Question link:

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

Question type: Euler Loop

Question:

My little sister had a beautiful neck?made of colorful beads. two successive beads in the neck?shared a common color at their meeting point. The figbelure ow shows a segment of the neck=:

But, alas! One day, the neck1_was torn and the beads were all scattered over the floor. my sister did her best to recollect all the beads from the floor, but she is not sure whether she was able to collect
All of them. now, she has come to me for help. she wants to know whether it is possible to make a neck1_using all the beads she has in the same way her original neck1_was made and if so in which order the bids must be put.

Please help me write a program to solve the problem.

Translation:

My sister has a necklace of various colors. The connector of two successive beads in the necklace shares the same color. For example, if the first bead is green + red, the beads must start with red + white.

Oh! Oh, my God! One day, the necklace was broken, and the beads fell to a place. My sister tried her best to pick up the beads one by one, but she was not sure whether all the beads were retrieved. Now, she asked me for help.

She wants to know if it is possible to put these beadsAllThe connection method is the same as the original method of the necklace.

Please help me write a program to solve this problem.

Sample input:

251 22 33 44 55 652 12 23 43 12 4

Sample output:

Case #1some beads may be lost Case #22 11 33 44 22 2

Ideas and summary:

This is the Euler Loop + print path,

This is very similar to the question of the UA 10129-play on words and Euler's path,

The difference is that the question is a directed graph, and this question is an undirected graph. The question is the Euler's path, and this question is the Euler's loop.

The difference between the Euler Road and the Euler loop is that the Euler loop starts from a certain point and returns to this point to form a loop. The Euler's path starts from one point and ends at another point.


The method for printing the Euler loop is described in detail on P112, "getting started with algorithms" by Liu rujia:

The following recursive functions are applicable to both Euler's path and loop. If you need to print the Euler's path, the parameter must be the start point of the path when the main program is called. In addition, the order of printing is in your order, so you actually use this copy.

In code, the printf statement should be replaced with a push statement, and the edge should be pushed 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);    }}

The code above is used for undirected graphs. If it is changed to Directed Graphs, change vis [u] [v] = vis [v] [u] = 1 to vis [u] [v] = 1.



This question may be repeated, for example:

1 2

2 2

2 2

2 2

2 3

3 1

1 2

2 1


So you need to change it a bit.

# Include <cstring> # include <iostream> # include <cstdio> # include <cmath> # include <stack> # define maxn 60 using namespace STD; int vis [maxn], CNT [maxn], G [maxn] [maxn], T, vis2 [maxn] [maxn], n, a, B; struct node {int U, V ;}; stack <node> st; void DFS (int u) {vis [u] = true; For (INT I = 0; I <maxn; ++ I) {If (G [u] [I] &! Vis [I]) DFS (I) ;}} void Euler (INT U) {for (INT V = 0; v <maxn; ++ V) if (G [u] [v]) {-- g [u] [v]; -- g [v] [u]; Euler (V); node T; T. U = u, T. V = V; // st. push (t); printf ("% d \ n", u, v) ;}} int main () {# ifdef local freopen ("input.txt ", "r", stdin); # endif int CAS = 1; scanf ("% d", & T); While (t --) {memset (G, 0, sizeof (g); memset (CNT, 0, sizeof (CNT); scanf ("% d", & N); For (INT I = 0; I <N; ++ I) {scanf ("% d", & A, & B); ++ G [a] [B]; ++ G [B] [a]; ++ CNT [a]; ++ CNT [B];} bool flag = true; For (INT I = 0; I <maxn; ++ I) {If (CNT [I] & 1) {flag = false; break ;}} printf ("case # % d \ n ", CAS ++); If (FLAG) {memset (VIS, 0, sizeof (VIS); memset (vis2, 0, sizeof (vis2); int flag2 = true; for (INT I = 0; I <maxn; ++ I) if (CNT [I]) {DFS (I); break ;}for (INT I = 0; I <maxn; ++ I) {If (CNT [I] &! Vis [I]) {flag2 = false; break ;}} if (flag2) {for (INT I = 0; I <maxn; ++ I) if (CNT [I]) {Euler (I); break;} // This can be recursively printed in reverse mode or saved to the stack for printing. // while (! St. empty () {// printf ("% d \ n", St. top (). u, St. top (). v); // st. pop (); //} else printf ("some beads may be lost \ n");} else {printf ("some beads may be lost \ n ");} if (t) printf ("\ n");} return 0 ;}



-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double



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.