It's easy to find the Euler loop. And output.
Only focus on using stacks to control output.
Why,
If the stack is not used, 1-> 2-> 3-> 1 is returned, and 4-> 5 is output. The connection fails, so if the stack is used, A complete path will be saved.
Because it is an undirected graph, as long as the degree of each point is even, there must be a legal Euler loop.
Summon code:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;int a[51][51],n=50,d[51],m,T;void dfs(int x){ for (int i=1; i<=n; i++) if (a[x][i]) { a[x][i]--,a[i][x]--; dfs(i); printf("%d %d\n",i,x); }}int main(){ int cas=0,U,V; scanf("%d",&T); while (T--) { memset(d,0,sizeof d); memset(a,0,sizeof a); scanf("%d",&m); while (m--) { scanf("%d%d",&U,&V); a[U][V]++,a[V][U]++; d[U]++,d[V]++; } bool ans=true; for (int i=1; i<=n; i++) if (d[i]&1) ans=false; if (cas) printf("\n"); printf("Case #%d\n",++cas); if (!ans) printf("some beads may be lost\n"); else dfs(U); } return 0;}