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.