Cod sursa(job #2866800)

Utilizator Madalin_IonutFocsa Ionut-Madalin Madalin_Ionut Data 9 martie 2022 23:27:23
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");

struct muchie
{
    int nod1, nod2;
};

int n, m;
vector<int> L[100003];
stack<muchie> st;
int lowest[100003], nivel[100003];
bitset<100003> viz;
vector<int> sol[100003];
int nrcb;

void Citire()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
}

void DFS(int nod, int tata)
{
    lowest[nod] = nivel[nod] = 1 + nivel[tata];
    viz[nod] = 1;
    for (int f : L[nod])
    {
        if (viz[f])
        {
            lowest[nod] = min(lowest[nod], nivel[f]);
            continue;
        }
        st.push({ nod, f });
        DFS(f, nod);
        lowest[nod] = min(lowest[nod], lowest[f]);
        if (nivel[nod] == lowest[f])
        {
            nrcb++;
            while (st.top().nod1 != nod || st.top().nod2 != f)
            {
                sol[nrcb].push_back(st.top().nod2);
                st.pop();
            }
            sol[nrcb].push_back(st.top().nod2);
            sol[nrcb].push_back(st.top().nod1);
            st.pop();
        }
    }
}

void Afisare()
{
    int i;
    fout << nrcb << "\n";
    for (i = 1; i <= nrcb; i++)
    {
        for (int elem : sol[i])
            fout << elem << " ";
        fout << "\n";
    }
    fout.close();
}

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