Link to this code:
https://cses.fi/paste/3a65d08738bc9b4da0ef5c/
#include <bits/stdc++.h>
#define uwu return 0;
using namespace std;
const int SIZE = 2e5 + 5;
vector <pair<int, int>> graph[SIZE];
vector <int> path;
#define fs first
#define sc second
bool been[SIZE];
bool check_able(int V){
for (int i = 1; i <= V; i++){
if(graph[i].size() & 1)
return 0;
}
return 1;
}
int cur[SIZE];
void dfs(int nd){
for (; cur[nd] < graph[nd].size(); cur[nd]++){
if(!been[graph[nd][cur[nd]].sc]){
been[graph[nd][cur[nd]].sc] = 1;
dfs(graph[nd][cur[nd]].fs);
}
}
path.push_back(nd);
return;
}
int main(){
cin.tie(0), ios::sync_with_stdio(0);
int V, E;
cin >> V >> E;
for (int u, v, i = 0; i < E; i++){
cin >> u >> v;
graph[u].push_back({v, i});
graph[v].push_back({u, i});
}
// check the amount of odd nodes
if(!check_able(V)){
cout << "IMPOSSIBLE\n";
uwu
}
// find cycle
dfs(1);
// check connectivity
if(path.size() != E + 1){
cout << "IMPOSSIBLE\n";
uwu
}
for(auto i:path){
cout << i << ' ';
}
cout << '\n';
uwu
}