This topic requires that all the edges in a undirected connected graph be divided into two pairs, which can only appear once, and one side must be connected together. vertices can be reused but edges cannot be reused.
The solvable condition is very easy, because the graph is connected, as long as the number of edges is an even number.
In the beginning, I directly searched for DFS brute-force attacks by using the Euler loop method, marking the path, and outputting the unlabeled two consecutive edges.
However, it turns out that this algorithm is wrong.
Violent search can be established only when the edge on the graph can exist in many edge pairs, but some images certainly do not meet this condition.
In fact, the solution is to consider the special side based on DFS.
That is, each time a vertex is located, the subnode is DFS. If a single edge is found under the subnode, the current edge and the subnode are merged and output.
Otherwise, the current edge will be stored in the queue that exclusive to the point. After all is saved, the current edge will be output in pairs. If there is a single side, return it to the father and tell him that there is a single side here.
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int N=100000+10;int u[N<<1],v[N<<1],nt[N<<1],ft[N];int n,m,cnt;int vis[N<<1];void add(int a,int b){ u[cnt]=a; v[cnt]=b; nt[cnt]=ft[a]; ft[a]=cnt++;}int dfs(int x,int f){ queue<int> vec; for (int i=ft[x];i!=-1;i=nt[i]){ int nx=v[i]; if (vis[i] || nx==f) continue; vis[i]=vis[i^1]=1; int r=dfs(nx,x); if (r){ printf("%d %d %d\n",x,nx,r); } else{ vec.push(nx); } } while (vec.size()>=2){ int a=vec.front(); vec.pop(); int b=vec.front(); vec.pop(); printf("%d %d %d\n",a,x,b); } if (!vec.empty()){ int a=vec.front(); vec.pop(); return a; } return 0;}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { cnt=0; memset(ft,-1,sizeof ft); memset(vis,0,sizeof vis); int a,b; for (int i=0;i<m;i++){ scanf("%d%d",&a,&b); add(a,b); add(b,a); } //cout<<m<<" "<<(m&1)<<endl; if (m&1){ puts("No solution"); continue; } dfs(1,-1); } return 0;}