Cod sursa(job #2696402)

Utilizator TaveNeagoe Gabriel-Octavian Tave Data 15 ianuarie 2021 20:21:32
Problema Componente biconexe Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.68 kb
#include<fstream>
#include<algorithm>
#include<vector>
#include<stack>
 
#define nmax 100005

using namespace std;

ifstream fin("biconex.in");
ofstream fout("biconex.out");
 
vector <int> list[nmax];
vector <vector <int>> comp_biconex;
stack <pair <int, int>> s;
 
int n, m, x, y, index=1, dindex[nmax]={0}, lowpoint [nmax];
 
 
void read(){
	
	fin>>n>>m;
 
	for(int i=1; i<=m; i++){
		fin>>x>>y;
		list[x].push_back(y);
		list[y].push_back(x);
	}
 
}
void articulatie (int v, int vsucc){

	int x=0, y=0;
	vector <int> afis;
	vector <int> :: iterator it;
	do{
		x = s.top().first;
		y = s.top().second;
		s.pop();
		afis.push_back(x), afis.push_back(y);
	}
	while(x!=v || y!=vsucc);
 
	comp_biconex.push_back(afis);
 
}

void biconex(int v = 1){

	dindex[v] = index ;
	lowpoint[v] = index ;
	index++;
 
	vector <int> :: iterator it ;
 
	for (it=list [v].begin(); it!=list [ v ].end(); it++){
		
		if (!dindex [*it]){
			s.push( make_pair(v, *it));
			biconex (*it);
			lowpoint [v] = min (lowpoint[v], lowpoint[*it]);
 
			if (lowpoint[*it] >= dindex[v])  
                articulatie (v, *it);
			
		}
		else{
			lowpoint[v] = min (lowpoint[v], dindex[*it]);
		}
	}
}		
void print(){

    fout<<comp_biconex.size()<<endl;

	vector <int> afis;
	vector <int> :: iterator it ;
 
	for(int i=0;  i<comp_biconex.size(); i++){

		afis=comp_biconex [i];
 
		sort(afis.begin(), afis.end());
		
		it=unique(afis.begin(), afis.end());
 
		afis.resize(it - afis.begin()); 
			
		for(int j=0; j<afis.size(); j++)
			fout<<afis[j]<<" ";
 
		fout<<endl;
		afis.clear();
	}
	
}
 
int main()
{
	read();
	biconex();
	print();
	return 0;
}