Cod sursa(job #2853099)

Utilizator alextmAlexandru Toma alextm Data 19 februarie 2022 21:18:40
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 1e5 + 5;

int timer, nrc;
int low[MAXN], lvl[MAXN];
bool viz[MAXN];
vector<int> G[MAXN], CC[MAXN], st;

void Componenta(int node, int last) {
	nrc++;
	while(st.back() != last)
		CC[nrc].push_back(st.back()), st.pop_back();
	CC[nrc].push_back(node);
}

void DFS(int node, int father = 0) {
	viz[node] = 1;
	lvl[node] = low[node] = ++timer;
	st.push_back(node);
	
	for(auto son : G[node])
		if(viz[son] == 0) {
			int last = st.back();
			DFS(son, node);
			low[node] = min(low[node], low[son]);
			if(lvl[node] <= low[son])
				Componenta(node, last);
		}
		else if(son != father)
			low[node] = min(low[node], lvl[son]);
}

int main() {
	int n, m;
	fin >> n >> m;
	
	while(m--) {
		int x, y;
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	
	DFS(1);
	
	fout << nrc << '\n';
	for(int i = 1; i <= nrc; i++) {
		for(auto node : CC[i])
			fout << node << ' ';
		fout << '\n';
	}
	
	return 0;
}