Poj1041 John's trip, returns the Euler's path in an undirected graph.

Source: Internet
Author: User

Click Open Link

Undirected Euler's loop:

1. Graph connectivity

2. The degrees of all vertices are even


# Include <cstdio> # include <cstring> # include <stack> # include <queue> # include <algorithm> using namespace STD; const int Mt = 2000; const int MS = 50; bool vis [MT + 5]; int G [MS] [MT + 5]; int deg [MS + 5]; INTF [MS + 5]; stack <int> S; int Street, startv; int findset (int x) {return f [x] = x? F [x]: F [x] = findset (F [x]);} void Union (int x, int y) {int FX = findset (X ); int FY = findset (y); If (FX! = FY) {f [FX] = FY ;}} bool used [MS]; bool check () {for (INT I = 1, CNT = 0; I <= MS; ++ I) if (used [I]) {If (deg [I] & 1) return false; If (F [I] = I) {If (CNT ++ = 1) return false ;}} return true ;}void Euler (int u) {for (INT I = 1; I <= Street; ++ I) if (G [u] [I]) {If (! Vis [I]) {vis [I] = 1; Euler (G [u] [I]); S. push (I) ;}}// PS: the graph is connected .... Int main () {int x, y, z; while (~ Scanf ("% d", & X, & Y) {If (x = 0 & Y = 0) break; scanf ("% d ", & Z); memset (G, 0, sizeof g); For (INT I = 0; I <= MS; ++ I) f [I] = I; memset (used, 0, sizeof used); memset (deg, 0, sizeof deg); Street = z; startv = min (x, y); deg [x] ++; DEG [y] ++; G [x] [Z] = y; G [y] [Z] = x; Union (x, y ); used [x] = used [y] = 1; while (~ Scanf ("% d", & X, & Y) {If (x = 0 & Y = 0) break; scanf ("% d ", & Z); Street = max (z, street); deg [x] ++; deg [y] ++; G [x] [Z] = y; G [y] [Z] = x; Union (x, y); used [x] = used [y] = 1;} If (! Check () {puts ("round trip does not exist."); continue;} else {memset (VIS, 0, sizeof vis); Euler (startv); While (! S. empty () {printf ("% d", S. top (); S. pop () ;}printf ("\ n") ;}} return 0 ;}


Poj1041 John's trip, returns the Euler's path in an undirected graph.

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.