Cod sursa(job #3328980)

Utilizator StefanRaresStefan Rares StefanRares Data 11 decembrie 2025 15:14:53
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#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;
vector<int> G[NMAX], CB[NMAX];
bool viz[NMAX];

void citire()
{
    int x, y;
    f >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}
void DFScb(int x, int tata)
{
    S[++vf] = x;
    viz[x] = 1;
    Niv[x] = Niv[tata] + 1;
    Nma[x] = Niv[x];
    for(const auto &y : G[x])
    {
        if(y == tata) continue;
        if(viz[y]) Nma[x] = min(Nma[x], Niv[y]);
        else
        {
            DFScb(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);
            }
        }
    }
}
void afis()
{
    g << nrCB << '\n';
    for(int i = 1; i <= nrCB; ++i, g << '\n')
        for(const auto &x : CB[i])
            g << x << ' ';
}

int main()
{
    citire();
    DFScb(1, 0);
    afis();
    f.close();
    g.close();
    return 0;
}