Problem Statement
Round Trip
Implementation
void solve() {
ll n, m;
cin >> n >> m;
vvll v(n);
rep(i, m) {
ll x, y;
cin >> x >> y;
--x, --y;
v[x].pb(y), v[y].pb(x);
}
ll st = -1, et = -1;
vll col(n, 0), p(n, -1);
function <bool(ll, ll)> dfs = [&](ll node, ll pr) {
debug(node);
col[node] = 1, p[node] = pr;
for (auto& child : v[node]) {
if (child == pr) continue;
if (col[child] == 0) {
if (dfs(child, node) == 1) return 1;
}
else if (col[child] == 1) {
st = child, et = node;
return 1;
}
}
col[node] = 2;
return 0;
};
rep(i, n) {
if (col[i] == 0) {
if (dfs(i, i)) {
debug(i);
vll path;
for (ll i = et; i != st; i = p[i]) path.pb(i);
path.pb(st);
path.pb(et);
cout << sz(path) << "\n";
rep(i, sz(path)) cout << path[i] + 1 << " ";
cout << "\n";
return;
}
}
}
cout << "IMPOSSIBLE\n";
}