Cod sursa(job #3322675)

Utilizator bogdan1479Luca Bogdan Alexandru bogdan1479 Data 15 noiembrie 2025 10:50:31
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 1e5 + 1;

struct muchie
{
    int x, y;

    muchie(int xx = 0, int yy = 0)
    {
        x = xx, y = yy;
    }
};

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

vector<int> G[NMAX], CB[NMAX];
bool viz[NMAX];

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

void citire()
{
    int x, y;
    fin >> N >> M;
    for(int i = 1; i <= M; ++i)
    {
        fin >> 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);
            }
        }
    }
}

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