分析:
基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍。
链式前向星构图,只要标记走过的单向边,边找边输出即可。
code
#include <iostream>
#include <cstdio>
using namespace std;
struct node {
int v, ne;
} edge[100009];
int head[10009], vis[100009], cnt = 1;
int n, m, x, y;
void addedge (int u, int v) {
edge[++cnt].v = v;
edge[cnt].ne = head[u];
head[u] = cnt;
}
void EulerianP (int x) {
for (int i = head[x]; i != 0; i = edge[i].ne) {
if (!vis[i]) {
vis[i] = 1;
EulerianP (edge[i].v);
}
}
printf ("%d\n", x);
}
int main()
{
scanf ("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf ("%d %d", &x, &y);
addedge (x, y);
addedge (y, x);
}
EulerianP (1);
return 0;
}