Cod sursa(job #2834715)

Utilizator stefanvoicaVoica Stefan stefanvoica Data 17 ianuarie 2022 15:30:38
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include<bits/stdc++.h>
#define int long long
using namespace std;
ifstream fin ("biconex.in");
ofstream fout("biconex.out");
const int dim = 100002;
int niv[dim], low[dim], st[dim], vf, nrc;
vector <int> sol[dim], a[dim];
bool viz[dim];

void dfs (int x,int tata)
{
    st[++vf] = x;
    viz[x] = 1;
    niv[x] = niv[tata] + 1;
    low[x] = niv[x];
    for (int y : a[x])
        if (viz[y])
        {
            if (low[x] > niv[y])
                low[x] = niv[y];
        }
        else
        {
            dfs (y, x);
            if (low[x] > low[y])
                low[x] = low[y];
            if (niv[x] <= low[y])
            {
                ++nrc;
                while (st[vf] != y)
                {
                    sol[nrc].push_back(st[vf]);
                    vf--;
                }
                --vf;
                sol[nrc].push_back(y);
                sol[nrc].push_back(x);
                //st[++vf] = x;
            }
        }
}

int32_t main()
{
    int n, m, x, y;
    fin >> n >> m;
    while (m--)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs (1, 0);
    fout << nrc << '\n';
    for (int i = 1; i <= nrc; i++)
    {
        for (int w : sol[i])
            fout << w << ' ';
        fout << '\n';
    }
}