제출 #648061

#제출 시각아이디문제언어결과실행 시간메모리
648061gokussjz어르신 집배원 (BOI14_postmen)C++17
100 / 100
497 ms58056 KiB
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define all(x)      x.begin(), x.end()
#define pb          push_back
#define sz(x)       (int)(x.size())
#define ll          long long
#define fi          first
#define se          second
#define lbd         lower_bound
#define ubd         upper_bound

template <typename T>
using ordered_set = tree<T, null_type,
      less<T>, rb_tree_tag,
      tree_order_statistics_node_update>;

const int MOD = 1e9 + 7;
const double eps = 1e-10;
const long long INF = 1e18;
const int N = 2e5 + 10;

struct Euler_Path {
	int n, m;
	vector<vector<pair<int, int>>> adj;
	vector<int> its;
	vector<pair<int, int>> ret, s;
	vector<int> path;
	vector<bool> vis;
	void init(int _n, int _m) {
		n = _n;
		m = _m;
		adj.resize(n);
		its.resize(n);
		vis.resize(m);
		s = {{0, -1}};
	}
	void add_edge(int u, int v, int id) {
		adj[u].pb({v, id});
		adj[v].pb({u, id});
	}
	vector<int> euler_dfs() {
		while (!s.empty()) {
			int x = s.back().first;
			while (its[x] < sz(adj[x]) && vis[adj[x][its[x]].second]) its[x]++;
			if (its[x] == sz(adj[x])) {
				if (!ret.empty() && ret.back().second != x) return path;
				ret.pb(s.back());
				s.pop_back();
			} else {
				s.pb({adj[x][its[x]].first, x});
				vis[adj[x][its[x]].second] = 1;
			}
		}
		if (sz(ret) != m + 1) return path;
		for (const auto &[v, id]: ret) {
			path.pb(v);
		}
		reverse(all(path));
		return path;
	}
} idk;

void solve() {
	int n, m;
	cin >> n >> m;
	idk.init(n, m);
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		--u, --v;
		idk.add_edge(u, v, i);
	}
	idk.euler_dfs();
	vector<int> ans = idk.path;
	vector<int> cycles;
	bool vis[n] = {};
	vector<vector<int>> fin;
	for (const auto &it : ans) {
		if (vis[it]) {
			vector<int> curr;
			curr.pb(it);
			while (cycles.back() != it) {
				curr.pb(cycles.back());
				vis[cycles.back()] = 0;
				cycles.pop_back();
			}
			fin.pb(curr);
		} else {
			vis[it] = 1;
			cycles.pb(it);
		}
	}
	for (const auto &v : fin) {
		for (const auto &it : v) cout << it + 1 << ' ';
		cout << '\n';
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int tt = 1;
	//cin >> tt;
	while (tt--) {
		solve();
		cout << '\n';
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...