Cod sursa(job #2646412)

Utilizator saeed_odakSaeed Odak saeed_odak Data 1 septembrie 2020 08:50:43
Problema Componente biconexe Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
// Oh damn, cann't you see I'm fine?

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

const int N = 2e5 + 5;

#define x first
#define y second

int n, m;
vector <int> g[N];

int cnt = 1, now = 1;
int st[N];
int seen[N];
int lowest_seen[N];
vector < vector <int> > ans;

void dfs(int u, int p) {
	st[now++] = u;
	lowest_seen[u] = seen[u] = cnt++;
	for(int v : g[u]) {
		if(!seen[v]) {
			int ptr = now;
			dfs(v, u);
			if(lowest_seen[v] >= seen[u]) {
				vector <int> res = {u};
				while(ptr < now) res.push_back(st[--now]);
				ans.push_back(res);
			}
			lowest_seen[u] = min(lowest_seen[u], lowest_seen[v]);
		}
		else if(v != p) {
			lowest_seen[u] = min(lowest_seen[u], seen[v]);
		}
	}
}

int main() {
	freopen("biconex.in", "r", stdin);
	freopen("biconex.out", "w", stdout);

	cin >> n >> m;
	for(int i=0; i<m; i++) {
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1, 0);
	cout << ans.size() << endl;
	for(auto con : ans) {
		for(int e : con) cout << e << " ";
		cout << endl;
	}

	return 0;
}