/* * POJ_1041.cpp * * Created on: 2013年11月18日 * Author: Administrator */ #include <iostream> #include <cstdio> using namespace std; const int maxn = 2000; const int maxm = 100; struct node { int s, t; } r[maxn];//边序列 bool vis[maxn];//边的访问标志序列 int deg[maxn], s[maxn];//deg[i]: 节点i的度数.s[] 欧拉回路的边序列 int n, S, stop;//n: 最大边序号,S: 最小节点序号, stop: 欧拉回路的边数 bool exist() {//判断是否存在欧拉回路.若存在度数为奇数的节点,则不可能存在欧拉回路 int i; for (i = 1; i < maxm; ++i) { if (deg[i] % 2 == 1) { return 0; } } return 1; } void dfs(int now) {//从now开始递归计算欧拉回路 int i; for (i = 1; i <= n; ++i) {//递归搜索与now相连&&未访问的边 if (!vis[i] && (r[i].s == now || r[i].t == now)) { vis[i] = 1;//访问第i条边 dfs(r[i].s + r[i].t - now);//访问该边的另一个端点 s[++stop] = i;//将第i条边填入欧拉回路 } } } int main() { int x, y, num; while (scanf("%d%d", &x, &y) != EOF, x || y) { S = min(x, y), n = 0; memset(deg, 0, sizeof(deg)); scanf("%d", &num); r[num].s = x; r[num].t = y; deg[x]++; deg[y]++; n = max(n, num); while (scanf("%d%d", &x, &y), x || y) { S = min(S, min(x,y)); scanf("%d", &num); r[num].s = x; r[num].t = y; deg[x]++; deg[y]++; n = max(n, num); } if(exist()){ stop = 0; memset(vis,0,sizeof(vis)); dfs(S); int i; for(i = stop; i >= 2 ; --i){//凡需输出欧拉回路 printf("%d ",s[i]); } printf("%d\n",s[1]); }else{ printf("Round trip does not exist.\n"); } } return 0; }