欧拉回路必须满足每个点的度都是偶数。
字典序的话只用按照编号排序一以后从大到小建边。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 51, M = 2010;
int n, m, deg[N];
int tot, Head[N], ver[M << 1], Next[M << 1], id[M << 1]; bool v[M];
struct rode { int x, y, id; } a[M];
int siz, path[M];
void add(int x, int y, int z) {
tot++;
ver[tot] = y;
id[tot] = z;
Next[tot] = Head[x];
Head[x] = tot;
}
bool cmp(rode p1, rode p2) { return p1.id > p2.id; }
void dfs(int x, int pre) {
for (int i = Head[x]; i; i = Head[x]) {
int y = ver[i]; Head[x] = Next[i];
if (v[i >> 1]) continue;
v[i >> 1] = 1;
dfs(y, id[i]);
}
path[++siz] = pre;
}
int main() {
while (1) {
memset(deg, 0, sizeof(deg));
tot = 1, memset(Head, 0, sizeof(Head));
memset(v, 0, sizeof(v));
siz = 0;
int x, y, z, st; n = 0;
for (m = 1;; m++) {
scanf("%d%d", &x, &y);
if (!x && !y) break;
if (m == 1) st = min(x, y);
n = max(n, max(x, y));
scanf("%d", &z);
a[m] = (rode){x, y, z};
deg[x]++, deg[y]++;
}
m--;
if (!m) break;
bool bk = 1;
for (int i = 1; i <= n; i++)
if (deg[i] & 1) { bk = 0; break; }
if (!bk) { puts("Round trip does not exist."); continue; }
sort(a + 1, a + m + 1, cmp);
for (int i = 1; i <= m; i++)
add(a[i].x, a[i].y, a[i].id), add(a[i].y, a[i].x, a[i].id);
dfs(st, 0); siz--;
for (int i = siz; i > 1; i--) printf("%d ", path[i]);
printf("%d\n", path[1]);
}
return 0;
}