Cod sursa(job #2686618)

Utilizator Gheorghita_VladGheorghita Vlad Gheorghita_Vlad Data 19 decembrie 2020 12:07:00
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
#define MAX 100005
using namespace std;
ifstream f("biconex.in");
ofstream g("biconex.out");
vector < int > v[MAX], sol[MAX];
bitset < MAX > c;
stack < int > st;
int n, m, x, y, nivel[MAX], nma[MAX], nr,i,j;
void dfs(int k, int tata)
{
    c[k] = true;
    st.push(k);
    for(int i = 0; i < v[k].size(); i++)
    {
        int vec = v[k][i];
        if(!c[vec])
        {
            nivel[vec] = nivel[k] + 1;
            nma[vec] = nivel[vec];
            dfs(vec, k);
            nma[k] = min(nma[k], nma[vec]);

            if(nivel[k] <= nma[vec])
            {
                nr++;
                sol[nr].push_back(k);
                while(st.top() != vec)
                {
                    sol[nr].push_back(st.top());
                    st.pop();
                }
                sol[nr].push_back(st.top());
                st.pop();
            }
        }
        else if(tata != vec)
            nma[k] = min(nma[k], nivel[vec]);

    }
}

int main()
{
    f>>n>>m;
    for(i = 1; i <= m; i++)
    {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1, 0);
    g<<nr<<"\n";
    for(i = 1; i <= nr; i++)
        {
            for(int j = 0; j < sol[i].size(); j++)
                g<<sol[i][j]<<" ";
            g<<'\n';
        }


}