Cod sursa(job #3246699)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 4 octombrie 2024 09:22:16
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

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

const int nmax = 100005;
int n, m, fr[nmax], nivel[nmax], nma[nmax], k;
vector<int> v[nmax], comp[nmax];
stack<int> St;

void compbicon(int nod, int dad)
{
    fr[nod] = 1; St.push(nod);
    nivel[nod] = nma[nod] = nivel[dad] + 1;

    for(auto x : v[nod])
        if(fr[x] && x != dad)
            nma[nod] = min(nma[nod], nivel[x]);

        else if(!fr[x])
        {
            compbicon(x, nod);
            nma[nod] = min(nma[nod], nma[x]);

            if(nivel[nod] <= nma[x])
            {
                k ++;
                while(St.top() != x)
                    comp[k].push_back(St.top()), St.pop();

                comp[k].push_back(x);
                comp[k].push_back(nod);
                St.pop();
            }
        }

}

int main()
{
    f >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int x, y; f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    compbicon(1, 0);

    g << k << '\n';
    for(int i = 1; i <= k; i ++, g << '\n')
        for(int j = 0; j < comp[i].size(); j ++)
            g << comp[i][j] << " ";
    return 0;
}