Cod sursa(job #2758215)

Utilizator Tudor_StefanaStefana Tudor Tudor_Stefana Data 8 iunie 2021 22:44:08
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("biconex.in");
ofstream fout("biconex.out");

bool v[100005];
int n, m, low[100005], lev[100005], t[100005];
vector<int> g[100005];
vector<vector<int> > comp;
stack<pair<int, int> > stk;

void add(int x, int y) {
	int a, b;
	vector<int> c;
	do {
		a = stk.top().first;
		b = stk.top().second;
		stk.pop();
		c.push_back(a);
		c.push_back(b);
	} while(!stk.empty() && (a != x || b != y));
	sort(c.begin(), c.end());
	comp.push_back(c);
}

void dfs(int x) {
	v[x] = true;
	lev[x] = low[x] = lev[t[x]]+1;
	for(auto next: g[x]) {
		if(!v[next]) {
			stk.push({x, next});
			t[next] = x;
			dfs(next);

			if(low[next] >= lev[x])
				add(x, next);

			low[x] = min(low[x], low[next]);
		} else if(next != t[x])
			low[x] = min(low[x], lev[next]);
	}
}

int main() {
	fin >> n >> m;
	while(m--) {
		int a, b;
		fin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(1);
	fout << comp.size() << '\n';
	for(auto c: comp) {
		for(int i = 0; i < c.size(); i++)
			if(i == 0 || c[i] != c[i-1])
				fout << c[i] << ' ';
		fout << '\n';
	}
}