Submission #534812

#TimeUsernameProblemLanguageResultExecution timeMemory
534812Bill_00Senior Postmen (BOI14_postmen)C++14
0 / 100
14 ms23756 KiB
#include <bits/stdc++.h>
#define N 500005
using namespace std;

int vis[N], p[N];
int n, m, pre; 
set<int> adj[N];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        adj[a].insert(b);
        adj[b].insert(a);
    }

    for(int i = 1; i <= n; i++){
        while(adj[i].size()){
            stack<int> s;
            s.push(i);
            while(s.size()){
                int now = s.top();
                // cout << now << ' ';
                
                if(vis[now] == 1){
                    // cout << 1 << '\n';
                    cout << now << ' ';
                    while(s.top() != now){
                        vis[s.top()] = 0;
                        cout << s.top() << ' ';
                        s.pop();
                    }
                    cout << '\n';
                    vis[now] = 0;
                }
                else{
                    if(adj[now].size() == 0){
                        vis[now] = 2;
                        s.pop();
                        // cout << 2 << '\n';
                    }
                    else{
                        // cout << 3 << '\n';
                        vis[now] = 1;
                        int k = *adj[now].begin();
                        adj[now].erase(k);
                        adj[k].erase(now);
                        s.push(k);
                    }
                }
            }

        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...