Cod sursa(job #1705520)

Utilizator valentin50517Vozian Valentin valentin50517 Data 20 mai 2016 18:48:52
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
vector<int> C[100100];
vector<pair<int,int> > A[100100];
bool B[100100],pod[200100];
int t,low[100100],lvl[100100],c,N,M;

void dfs(int v, int f){
	low[v] = lvl[v] = ++t;
	B[v] = true;
	for(auto el : A[v]){
		if(el.x == f) continue;
		if(B[el.x]) low[v] = min(low[v],low[el.x]);
		else{	
			dfs(el.x,v);
			low[v] = min(low[el.x],low[v]);
			if(low[el.x] > lvl[v]) pod[el.y] = true; 
		}
	}
}

void colect(int v){
	C[c].push_back(v);
	B[v] = true;
	for(auto el : A[v]) if(!pod[el.y] && !B[el.x]) colect(el.x);
}

int main(){
	fin >> N >> M;
	for(int x,y,i = 1;i<=M;i++){
		fin >> x >> y;
		A[x].push_back(make_pair(y,i));
		A[y].push_back(make_pair(x,i));
	}
	dfs(1,0);
	memset(B,0,sizeof(B));
	for(int i = 1;i<=N;i++){
		c++;
		colect(i);
		if(C[c].size() == 1) C[c].clear(),c--;
	}
	for(int i = 1;i<=N;i++)
		for(auto el:A[i]) if(pod[el.y]) C[++c].push_back(i),C[c].push_back(el.x),pod[el.y] = false;
	
	fout << c << '\n';
	for(int i = 1;i<=c;i++){
		for(auto el : C[i]) fout << el << ' ';
		fout << '\n';
	}
	
	return 0;
}