Cod sursa(job #3224152)

Utilizator Mihai_999Diaconeasa Mihai Mihai_999 Data 14 aprilie 2024 20:18:42
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <vector>
#define nl '\n'

using namespace std;
const int NMAX = 100001;

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

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 DFS(int x, int tata)
{
    S[++vf] = x;
    viz[x] = 1;
    Niv[x] = Niv[tata]+1;
    Nma[x] = Niv[x];
    for (const int &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);
    fout << nrCB << nl;
    for (int i = 1; i <= nrCB; i++)
    {
        for (const int &x : CB[i])
            fout << x << ' ';
        fout << nl;
    }
    return 0;
}