[POJ 1041] John's Trip
[题目链接]
http://poj.org/problem?id=1041
[算法]
欧拉回路
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 2010 #define MAXM 45 int i,u,v,cnt,max_id,id; int degree[MAXN],ans[MAXN]; bool visited[MAXN]; pair<int,int> e[MAXN]; bool flag; inline void dfs(int u) { int i,v; for (i = 1; i <= max_id; i++) { if (!visited[i] && (e[i].first == u || e[i].second == u)) { visited[i] = true; dfs(e[i].first + e[i].second - u); ans[++cnt] = i; } } } int main() { while (scanf("%d%d",&u,&v) != EOF) { if (!u && !v) break; max_id = 0; memset(degree,false,sizeof(degree)); memset(visited,false,sizeof(visited)); scanf("%d",&id); max_id = max(max_id,id); e[id] = make_pair(u,v); degree[u]++; degree[v]++; while (scanf("%d%d",&u,&v) && (u || v)) { scanf("%d",&id); e[id] = make_pair(u,v); max_id = max(max_id,id); degree[u]++; degree[v]++; } flag = false; for (i = 1; i <= 50; i++) { if (degree[i] & 1) { flag = true; break; } } if (flag) { printf("Round trip does not exist.\n"); continue; } cnt = 0; dfs(1); for (i = cnt; i >= 1; i--) printf("%d ",ans[i]); printf("\n"); } return 0; }