Cod sursa(job #2896913)

Utilizator matthriscuMatt . matthriscu Data 1 mai 2022 14:53:19
Problema Componente biconexe Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize ("O3")

#define NMAX 100005
#define problem "biconex"

int n, m, d[NMAX], low[NMAX];
vector<int> G[NMAX];
stack<pair<int, int>> st;
vector<vector<int>> ans;

void solve(int i, int depth) {
	d[i] = low[i] = depth;

	for (int neigh : G[i])
		if (!d[neigh]) {
			st.push({i, neigh});
			solve(neigh, depth + 1);
			low[i] = min(low[neigh], low[i]);
			if (low[neigh] >= d[i]) {
				ans.push_back({});
				pair<int, int> curr;
				do {
					curr = st.top();
					st.pop();
					ans.back().push_back(curr.first);
					ans.back().push_back(curr.second);
				} while (curr != make_pair(i, neigh));
				sort(ans.back().begin(), ans.back().end());
				unique(ans.back().begin(), ans.back().end());
			}
		} else
			low[i] = min(d[neigh], low[i]);
}

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

	scanf("%d%d", &n, &m);


	for (int i = 1, x, y; i <= m; ++i) {
		scanf("%d%d", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}

	for (int i = 1; i <= n; ++i)
		if (!d[i])
			solve(i, 1);

	printf("%ld\n", ans.size());

	for (vector<int> &v : ans) {
		for (int i : v)
			printf("%d ", i);
		puts("");
	}
}