Pagini recente » Cod sursa (job #1045774) | Cod sursa (job #2342274) | Cod sursa (job #2218373) | Cod sursa (job #3258413) | Cod sursa (job #960242)
Cod sursa(job #960242)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <stack>
#include <fstream>
#include <iostream>
#include <algorithm>
#define NMAX 100005
using namespace std;
int N,M,x,y, timp = 0, t = -1, inStack[NMAX], nod;
vector<int> neigh[NMAX];
vector<int> d( NMAX, -1);
vector<int> low( NMAX, -1);
vector<int> components[NMAX];
vector< pair<int,int> > s;
void explorare( int i ){
timp++;
d[i] = timp;
low[i] = timp;
inStack[i] = 1;
for( unsigned k = 0; k < neigh[i].size(); k++ ){
if( d[ neigh[i][k] ] == -1 ){ // nu a mai fost vizitat
s.push_back( make_pair( i, neigh[i][k] ) );
explorare( neigh[i][k] );
if( low[i] > low[ neigh[i][k] ] )
low[i] = low[ neigh[i][k] ]; // extrag minimul dintre low[i] si low[neigh[i][k]]
if( low[ neigh[i][k] ] >= d[i] ){
++t;
pair<int,int> muchie;
do{
muchie = s.back();
s.pop_back();
components[t].push_back( muchie.first );
components[t].push_back( muchie.second );
//inStack[muchie.first] = 0;
//inStack[muchie.second] = 0;
if( muchie.first == i && muchie.second == neigh[i][k] )
break;
}while( 1 );
}
}
else{
if( inStack[ neigh[i][k] ] == 1 ) // daca a fost vizitat si nodul e si in stiva
low[i] = ( low[i] > d[ neigh[i][k] ] ? d[neigh[i][k]] : low[i] ); // iau min dintre idx[neigh[i][k]] si b[i]
}
}
}
void dfs(){
for( int i = 1; i <= N; i++ )
if( d[i] == -1 ){ // nu a fost vizitat
explorare( i );
}
}
int main(){
freopen("biconex.in", "r", stdin );
freopen("biconex.out", "w", stdout );
scanf("%d%d",&N, &M );
for( int i = 1; i <= M ; i++ ){
scanf("%d%d",&x,&y);
neigh[x].push_back(y);
neigh[y].push_back(x);
}
dfs();
printf("%d\n", t+1);
for( int i = 0; i <= t; i++){
sort( components[i].begin(), components[i].end() );
components[i].erase(unique(components[i].begin(),components[i].end()), components[i].end());
for( unsigned k = 0; k < components[i].size(); k++)
printf("%d ", components[i][k] );
printf("\n");
}
return 0;
}