Cod sursa(job #2744477)

Utilizator chriss_b_001Cristian Benghe chriss_b_001 Data 24 aprilie 2021 18:55:26
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("biconex.in");
ofstream g("biconex.out");

const int NMAX = 100001;

int N, M,
    nrCB,
    Niv[NMAX], Nma[NMAX],
    S[NMAX], vf;
bool viz[NMAX];

vector<int> G[NMAX];
vector<int>CB[NMAX];

void citire()
{
    f >> N >> M;
    for(int i = 1; i <= M; ++i)
    {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void DFS(int x, int tata)
{
    S[++vf] = x;
    viz[x] = 1;
    Niv[x] = Niv[tata] + 1;
    Nma[x] = Niv[x];
    for(auto &y : G[x])
    {
        if(y == tata)continue;
        if(viz[y])
            Nma[x] = min(Nma[x], Niv[y]);
        else
        {
            DFS(y, x);
            Nma[x] = min(Nma[x], Nma[y]);
            if(Niv[x] <= Nma[y])
            {
                ++nrCB;
                while(S[vf] != y)
                    CB[nrCB].push_back(S[vf--]);
                CB[nrCB].push_back(y);
                --vf;
                CB[nrCB].push_back(x);
            }
        }
    }
}

int main()
{
    citire();
    DFS(1, 0);
    g << nrCB << '\n';
    for(int i = 1; i <= nrCB; ++i)
    {
        for(auto &x : CB[i])
            g << x << ' ';
        g << '\n';
    }
    return 0;
}