Test instructions
The Euler path to the smallest dictionary order is given for an image without a direction.
Analysis:
Select the lowest numbered and not visited edges for deep search.
Code:
POJ 1041//sep9#include <iostream> #include <vector> #include <algorithm>using namespace Std;const int maxn=2000;const int maxm=4000;vector< pair<int,int> > adj[maxn+4];vector<int> path;int F[maxN]; BOOL vis[maxn];void Add (int x,int y,int z) {adj[x].push_back (Make_pair (z,y)); Adj[y].push_back (Make_pair (z,x));} int find (int u) {return f[u]==u?u:f[u]=find (F[u]);} void Dfs (int u) {int i;for (i=0;i<adj[u].size (); ++i) {int e=adj[u][i].first;int v=adj[u][i].second;if (!vis[e]) {vis[ E]=true;dfs (v);p Ath.push_back (e);}} BOOL Solve () {int i,j;for (i=1;i<maxn;++i) f[i]=i;for (i=1;i<maxn;++i) for (J=0;j<adj[i].size (); ++j) {int u=i,v =adj[i][j].second;int pu=find (U), Pv=find (v); if (PU!=PV) F[PU]=PV;} int Origin=-1;for (i=1;i<maxn;++i) if (Adj[i].size ()) {if (Adj[i].size ()%2==1) return false;if (origin==-1) origin=i; if (Find (i)!=find (origin)) return False;sort (Adj[i].begin (), Adj[i].end ());} Path.clear (); memset (vis,false,sizeof (VIS)), if (Origin!=-1) Dfs (origin); reverse (Path.begin (), PATh.end ()); return true;} int main () {int I,j,x,y,z,end;end=0;while (1) {scanf ("%d%d", &x,&y), if (x==0&&y==0) {if (end==0) {if ( Solve () ==false) printf ("Round trip does not exist.\n"), Else{for (I=0;i<path.size (); ++i) printf ("%d", path[i]); printf ("\ n");} for (I=1;i<=maxn;++i) adj[i].clear (); end=1;} Elsebreak;} ELSE{END=0;SCANF ("%d", &z); Add (x, y, z);}} return 0;}
POJ 1041 John ' s trip Euler circuit