题目链接:http://poj.org/problem?id=1041
题目:
题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发又回到起点的字典序最小的路径,如果达不到输出Round trip does not exist.
思路:首先得判断是否存在欧拉回路,如果不存在则输出“Round trip does not exist.”。记录每个路口的度,如果存在度为奇数得路口则是不存在欧拉回路得图,否则用mp[u][t]=v来表示u可以通过t这条街道到达v,跑一边欧拉回路并记录路径即可。
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 typedef long long ll; 17 typedef pair<ll, ll> pll; 18 typedef pair<ll, int> pli; 19 typedef pair<int, ll> pil;; 20 typedef pair<int, int> pii; 21 typedef unsigned long long ull; 22 23 #define lson i<<1 24 #define rson i<<1|1 25 #define bug printf("********* "); 26 #define FIN freopen("D://code//in.txt", "r", stdin); 27 #define debug(x) cout<<"["<<x<<"]" <<endl; 28 #define IO ios::sync_with_stdio(false),cin.tie(0); 29 30 const double eps = 1e-8; 31 const int mod = 10007; 32 const int maxn = 1e6 + 7; 33 const double pi = acos(-1); 34 const int inf = 0x3f3f3f3f; 35 const ll INF = 0x3f3f3f3f3f3f3f; 36 37 int s, u, v, t, mx, p; 38 int mp[55][2007], in[55], vis[2007], ans[2007]; 39 40 void eulergraph(int s) { 41 for(int i = 1; i <= mx; i++) { 42 if(mp[s][i] && !vis[i]) { 43 vis[i] = 1; 44 eulergraph(mp[s][i]); 45 ans[++p] = i; 46 } 47 } 48 } 49 50 int main() { 51 //FIN; 52 while(~scanf("%d%d", &u, &v)) { 53 if(u == 0 && v == 0) break; 54 s = min(u, v); 55 p = 0; 56 memset(in, 0, sizeof(vis)); 57 memset(mp, 0, sizeof(mp)); 58 memset(vis, 0, sizeof(vis)); 59 scanf("%d", &t); 60 in[u]++, in[v]++; 61 mx = t; 62 mp[u][t] = v, mp[v][t] = u; 63 while(~scanf("%d%d", &u, &v)) { 64 if(u == 0 && v == 0) break; 65 scanf("%d", &t); 66 mx = max(mx, t); 67 in[u]++, in[v]++; 68 mp[u][t] = v, mp[v][t] = u; 69 } 70 int flag = 0; 71 for(int i = 1; i <= 45; i++) { 72 if(in[i] & 1) { 73 printf("Round trip does not exist. "); 74 flag = 1; 75 break; 76 } 77 } 78 if(flag) continue; 79 eulergraph(s); 80 for(int i = p; i >= 1; i--) { 81 printf("%d%c", ans[i], i == 1 ? ' ' : ' '); 82 } 83 } 84 return 0; 85 }