Cod sursa(job #2726985)

Utilizator Uriesu_IuliusUriesu Iulius Uriesu_Iulius Data 21 martie 2021 13:21:46
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, nrc;
vector<int> G[100001], cbc[100001];
int dep[100001], nma[100001];
bool ap[100001];
stack<int> stiva;

void Citire()
{
    int i, j;
    fin >> n >> m;
    for(int c=0; c<m; c++)
    {
        fin >> i >> j;
        G[i].push_back(j);
        G[j].push_back(i);
    }
}

void dfs(int k, int t)
{
    ap[k]=1;
    stiva.push(k);
    dep[k]=dep[t]+1;
    nma[k]=dep[k];
    for(int x : G[k])
        if(x!=t)
        {
            if(ap[x])
            {
                if(dep[x]<nma[k])
                    nma[k]=dep[x];
            }
            else
            {
                dfs(x, k);
                if(nma[x]<nma[k])
                    nma[k]=nma[x];
                if(dep[k]<=nma[x])
                {
                    nrc++;
                    while(stiva.top()!=x)
                    {
                        cbc[nrc].push_back(stiva.top());
                        stiva.pop();
                    }
                    cbc[nrc].push_back(x);
                    stiva.pop();
                    cbc[nrc].push_back(k);
                }
            }
        }
}

void Afisare()
{
    fout << nrc << "\n";
    for(int i=1; i<=nrc; i++)
    {
        for(int k : cbc[i])
            fout << k << " ";
        fout << "\n";
    }
}

int main()
{
    Citire();
    dfs(1, 0);
    Afisare();
    return 0;
}