Cod sursa(job #1384064)

Utilizator koroalinAlin Corodescu koroalin Data 10 martie 2015 20:35:58
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <fstream>
#include <vector>
#include <set>
#define NMAX 100001
#define MMAX 200001
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int n,m,dfn[NMAX],low[NMAX],nrfii,vf,nrb,nr;
pair <int,int> stiva[MMAX];
bool art[NMAX],uz[NMAX];
vector <int> G[NMAX];
set <int> sol[MMAX];
void citire();
void dfs(int x,int tx,int nr);
void afiseaza(int x,int tx);
int main()
{
    int i;
    citire();
    stiva[++vf]=make_pair(1,0);
    dfs(1,0,1);
    fout<<nrb<<'\n';
    for ( i=1; i<=nrb; i++)
    {
        set<int>::iterator it;
        for (it=sol[i].begin(); it!=sol[i].end(); ++it)
            fout<<*it<<' ';
        fout<<'\n';
    }
    return 0;
}
void citire()
{
    int a,b,i;
    fin>>n>>m;
    for (i=1; i<=m; i++)
    {
        fin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
}
void dfs(int x,int tx,int nr)
{
    int i;
    dfn[x]=nr; low[x]=dfn[x];
    //art[x]=0;
    for (i=0; i<G[x].size(); ++i)
    {
        if (G[x][i]==tx) continue;
        if (!dfn[G[x][i]])
        {
            stiva[++vf]=make_pair(G[x][i],x);
            dfs(G[x][i],x,nr+1);
            if (low[x]>low[G[x][i]]) low[x]=low[G[x][i]];
            if (low[G[x][i]]>=dfn[x])
                afiseaza(G[x][i],x);
        }
        else
            if (low[x]>dfn[G[x][i]])
                low[x]=dfn[G[x][i]];
    }
}
void afiseaza(int x,int tx)
{
    pair <int,int> p;
    nrb++;
    do
    {
        p=stiva[vf];
        sol[nrb].insert(p.first); sol[nrb].insert(p.second);
        vf--;
    }
    while ( !(p.first==x && p.second==tx) );
}