Submission #535409

#TimeUsernameProblemLanguageResultExecution timeMemory
535409ShelterSenior Postmen (BOI14_postmen)C++17
55 / 100
658 ms130464 KiB
#include <bits/stdc++.h>
 
#define len(x) (int)(x).size()
#define range(i, x, y) for(int i=x; i<y; i++)
#define sws ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vint vector<int>
#define vll vector<long long>
#define vstr vector<string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define pf push_front
#define del(a, n) (a).erase(a.begin()+(n))
#define mp make_pair
#define all(a) (a.begin(), a.end())
const int MAX = 1e6;
 
using namespace std;

vint sol;

void hier(unordered_set<int> gra[], int n, int m, int now){
    int next = now;
    stack<int> path;
    int value_edge[n+1];
    for(int i = 0; i <= n; i++){
        value_edge[i] = gra[i].size();
    }
    path.push(now);
    while(!path.empty()){
        if(value_edge[now]){
            path.push(now);
            for(auto x : gra[now]){
                next = x;
                break;
            }
            gra[now].erase(next);
            gra[next].erase(now);
            value_edge[now] --; 
            value_edge[next] --;
            now = next;
        } else{
            sol.push_back(now);
            now = path.top();
            path.pop();
        }
    }
}

bool vs[MAX];
void decompose() { 
	stack<int> cur; 
	vector<vint> re; 
	for(int u:sol) { 
		if(!vs[u]) { 
			cur.push(u); 
			vs[u] = 1; 
		}
		else { 
			vint cyc; 
			while(vs[u]) { 
				cyc.pb(cur.top()); 
				vs[cur.top()] = 0; 
				cur.pop(); 
			}
			for(auto x : cyc){
                cout << x << " ";
            }
            cout << endl;
			cur.push(u); 
			vs[u] = 1; 
		}
	}
}

int main(){
    sws
    int n, m;
    cin >> n >> m;
    unordered_set<int> gra[n+1];
    for(int i=0; i<m; i++){
        int u, v;
        cin >> u >> v;
        gra[u].insert(v);
        gra[v].insert(u);
    }
    hier(gra, n, m, 1);
    decompose();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...