Cod sursa(job #1645669)

Utilizator dorin31Geman Dorin Andrei dorin31 Data 10 martie 2016 13:14:59
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define maxN 100001

using namespace std;

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

int n, m, lvl[maxN], low[maxN];
vector <int> G[maxN];
vector < vector<int> > BC;
stack < pair<int,int> > S;

void citire()
{
    int x,y;
    fin>>n>>m;
    while (m--)
    {
        fin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void getCB(int x, int y)
{
    vector <int> crtBC;
    pair <int, int> Now;
    do {
        Now=S.top();
        S.pop();
        crtBC.push_back(Now.second);
    } while (Now.first != x && Now.second !=y);
    crtBC.push_back(Now.first);
    BC.push_back(crtBC);
}

void DFS(int x, int f=0)
{
    low[x]=lvl[x];
    for (int i=0; i<G[x].size(); ++i)
    {
        int nv=G[x][i];
        if (nv==f) continue;

        if (!lvl[nv])
        {
            lvl[nv]=lvl[x]+1;
            S.push(make_pair(x,nv));
            DFS(nv,x);
            low[x]=min(low[x],low[nv]);
            if (lvl[x]<=low[nv])
                getCB(x,nv);
        }
        else low[x]=min(low[x], lvl[nv]);
    }
}

int main()
{
    citire();
    for (int i=1; i<=n; ++i)
        if (!lvl[i]) {
            lvl[i]=1;
            DFS(i);
    }
    fout<<BC.size()<<'\n';
    for (int i=0; i<BC.size(); ++i)
    {
        for (int j=0; j<BC[i].size(); ++j)
            fout<<BC[i][j]<<' ';
        fout<<'\n';
    }
    return 0;
}