If there is a stroke in a graph, the path of a stroke is called the Euler Road, and if you finally return to the starting point, then this path is called the Euler loop.
Here is a definition: singular point refers to the number of edges connected to this point has an odd number of points, conversely, an even number is called even point.
So how to find the Euler (back) path in a diagram, there are two theorems:
1. Conditions for the existence of the Euler road: the graph is connected, and there are only two singular points
2. Conditions for the presence of a Euler loop: the graph is connected and there are 0 singularities
Therefore, in order to find the Euler circuit, the depth-first traversal can be performed on any one point, and the Euclidean circuit is searched, then the depth-first traversal of a singularity is performed, and the time complexity is O (m+n).
Code:
1#include <iostream>2#include <cstring>3 using namespacestd;4 #defineMAXN 1015 intG[MAXN][MAXN],DU[MAXN],CIRCUIT[MAXN];6 intN,e,circuitpos,i,j,x,y,start;7 voidFind_circuit (inti)8 {9 for(j=1; j<=n;j++)Ten if(g[i][j]==1) One { Ag[i][j]=g[j][i]=0; - Find_circuit (j); - } thecircuit[++circuitpos]=i; - } - intMain () - { +Memset (G,0,sizeof(g)); -Cin>>n>>e; + for(i=1; i<=e;++i) A { atscanf"%d%d",&x,&y); -g[x][y]=g[y][x]=1; -du[x]++;d u[y]++; - } -start=1; - for(i=1; i<=n;++i) in if(du[i]%2==1) -start=i; tocircuitpos=0; + find_circuit (start); - for(i=1; i<=circuitpos;++i) theprintf"%d", Circuit[i]); *}View Code
Euler (back) road