제출 #851950

#제출 시각아이디문제언어결과실행 시간메모리
851950NK_어르신 집배원 (BOI14_postmen)C++17
0 / 100
1 ms604 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 18;

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int N, M; cin >> N >> M;

	V<vpi> adj(N); vi E(M);
	vi cur(N, 0);
	for(int i = 0; i < M; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(mp(v, i));
		adj[v].pb(mp(u, i));
	}
	
	V<vi> ans;

	vi stk; vi vis(N);
	function<void(int)> dfs = [&](int u) {
		stk.pb(u); vis[u] = 1;
		// cerr << "ENTER " << u << endl;
		// cerr << "STK ";
		// for(auto& x : stk) cerr << x << " ";
		// cerr << endl;

		bool done = 1;
		while(cur[u] < sz(adj[u])) {
			int v, i; tie(v, i) = adj[u][cur[u]++];
			
			if (E[i]) continue;
			done = 0; E[i] = 1;

			if (vis[v]) {
				vi cyc;
				for(int x = -1; x != v; ) {
					cyc.pb(x = stk.back()); stk.pop_back();
					vis[x] = 0;
				}
				// cerr << "CYC ";
				// for(auto& x : cyc) cerr << x << " ";
				// cerr << nl;


				ans.pb(cyc); stk.pb(v);
				return;
			}

			dfs(v);

			if (stk.back() != u) return;
		}

		if (done) { 
			// cerr << "DONE " << u << endl;
			stk.pop_back(); vis[u] = -1; 
		}

		// cerr << "LEFT " << u << endl;
	};

	for(int i = 0; i < N; i++) {
		// cerr << i << " => " << vis[i] << endl;
		if (vis[i] != -1) dfs(i);
	}

	for(auto& v : ans) {
		for(auto& x : v) cout << x + 1 << " ";
		cout << nl;
	}

	exit(0-0);
} 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...