Link to this code:
https://cses.fi/paste/00664a2db6e5c6bfdba158//* 777 */
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v, w;
Edge(int from, int to, int weight) : u(from), v(to), w(weight) {}
};
const int MAX_N = 2e6 + 5, INF = 1e9 + 5;
int N, M, parent[MAX_N];
vector<pair<int, int>> g[MAX_N]; // adj list : g[u] is list of {v, w}
bitset<MAX_N> vis;
vector<int> cycle_path;
void add_edge(int u, int v, int w = 1, bool dir = false) {
g[u].emplace_back(v, w);
if (!dir) {
g[v].emplace_back(u, w);
}
}
void build_graph(const vector<Edge>& edges, bool dir = false) {
for (auto& e : edges) {
add_edge(e.u, e.v, e.w, dir); // (from, to, weight, isDirected)
}
}
vector<int> get_path(int src, int dst) {
vector<int> path;
while (dst && (path.empty() || dst != src)) {
path.push_back(dst);
dst = parent[dst];
}
path.push_back(src);
reverse(path.begin(), path.end());
return path;
}
bool has_cycle_BFS_undir(int src = 1) {
queue<pair<int, int>> q;
q.push({src, -1});
vis[src] = 1;
while (!q.empty()) {
auto& [u, par] = q.front();
q.pop();
for (auto& [v, w] : g[u]) {
if (v == par) continue;
if (vis[v]) {
vector<int> p1 = get_path(src, u);
vector<int> p2 = get_path(src, v);
int i = 0;
while (p1[i + 1] == p2[i + 1]) ++i;
cycle_path.insert(cycle_path.end(), p1.begin() + i, p1.end());
cycle_path.insert(cycle_path.end(), p2.rbegin(), p2.rend() - i);
return 1;
}
parent[v] = u;
vis[v] = 1;
q.push({v, u});
}
}
return 0;
}
void solve() {
cin >> N >> M;
vector<Edge> edges;
edges.reserve(M);
for (int u, v, i = 0; i < M; ++i) {
cin >> u >> v;
edges.emplace_back(u, v, 1);
}
build_graph(edges, false);
bool has_cycle = 0;
for (int i = 1; i <= N && !has_cycle; ++i) {
if (vis[i]) continue;
has_cycle |= has_cycle_BFS_undir(i);
}
if (!has_cycle) {
cout << "IMPOSSIBLE";
return;
}
cout << (int) cycle_path.size() << '\n';
for (int x : cycle_path) cout << x << " ";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}