#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adj, ans;
map<pair<int, int>, bool> streetCovered;
vector<bool> visited;
void showCurrAns(){
    cout << "Curr ans: \n";
    for(auto u : ans.back()){
        cout << u+1 << " ";
    }
    cout << "\n";
}
void showVisited(){
    cout << "Visited: \n";
    for(auto u : visited){
        cout << u << " ";
    }
    cout << "\n";
}
void showStreetCovered(){
    cout << "Streets covered: \n";
    for(auto u : streetCovered){
        cout << u.first.first+1 << " und " << u.first.second+1 << " = " << u.second << "\n";
    }
}
void dfs(int x){
    if(visited[x]){
        visited[x]=false;
        vector<int> currAns = {x};
        //showCurrAns();
        while(!ans.back().empty() && ans.back().back()!=x){
            currAns.push_back(ans.back().back());
            visited[ans.back().back()]=false;
            ans.back().pop_back();
        }
        if(!ans.back().empty()) ans.back().pop_back();
        if(ans.back().empty()){
            swap(currAns, ans.back());
            return;
        }
        else{
            ans.push_back(currAns);
            swap(ans.back(), ans[ans.size()-2]);
        }
        //showCurrAns();
        //showVisited();
    }
    visited[x]=true;
    ans.back().push_back(x);
    while(!adj[x].empty() && streetCovered[{x, adj[x].back()}]) adj[x].pop_back();
    int u = adj[x].back();
    streetCovered[{u, x}]=true;
    streetCovered[{x, u}]=true;
    dfs(u);
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    adj.resize(n); visited.resize(n, false);
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v; u--; v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    int j = 0;
    while(j<n){
        while(!adj[j].empty() && streetCovered[{j, adj[j].back()}]) adj[j].pop_back();
        if(adj[j].empty()){
            j++;
            continue;
        }
        ans.push_back({});
        dfs(j);
        if(adj[j].empty()) j++;
    }
    for(int i = 0; i < ans.size(); i++){
        for(int j = 0; j < ans[i].size(); j++){
            cout << ans[i][j]+1 << " ";
        }
        cout << "\n";
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |