#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 1e2 + 10, MAXM = 2e3 + 10;
vector<pair<int, int>> g[MAXN];
int vis[MAXM];
int n, m;
int deg[MAXN];
bool flag = false;
int seq[MAXM];
int top;
int r;
void dfs(int u, int cnt) {
for (auto e : g[u]) {
int id = e.first, v = e.second;
if (vis[id])
continue;
vis[id] = 1;
dfs(v, id);
}
if (cnt)
seq[++top] = cnt;
}
void solve() {
int x, y, id;
while (cin >> x >> y && x) {
++m;
cin >> id;
g[x].push_back({id, y});
g[y].push_back({id, x});
n = max({n, x, y});
deg[x]++;
deg[y]++;
}
for (int i = 1; i <= n; ++i) {
sort(g[i].begin(), g[i].end());
if (deg[i] % 2) {
cout << "Round trip does not exist.\n";
return;
}
}
flag = false;
top = 0;
dfs(r, 0);
reverse(seq + 1, seq + 1 + m);
for (int i = 1; i <= m; ++i)
cout << seq[i] << " \n"[i == m];
// cout<<'\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int x, y;
while (cin >> x >> y && x) {
r = min(x, y);
for (int i = 1; i <= n; ++i)
g[i].clear();
memset(vis, 0, sizeof vis);
memset(deg, 0, sizeof deg);
n = 0;
m = 1;
int id;
cin >> id;
deg[x]++;
deg[y]++;
g[x].push_back({id, y});
g[y].push_back({id, x});
n = max(x, y);
solve();
}
}
一本通1531:John‘s Trip
于 2024-03-05 21:30:25 首次发布
这篇文章展示了如何使用C++实现一个算法,通过深度优先搜索找出给定图中的回路。它处理输入的边并判断是否存在回路,如果存在则输出每个回路的长度。
892

被折叠的 条评论
为什么被折叠?



