Cod sursa(job #2856135)

Utilizator monica_LMonica monica_L Data 23 februarie 2022 13:57:50
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, nr;
int niv[100001], low[100001];
vector<int> v[100001], sol[100001];
stack<int> s;
bool viz[100001];

void dfs(int i, int level)
{
    int x;
    viz[i] = 1;
    niv[i] = low[i] = level;
    s.push(i);

    for(auto vecin : v[i])
        if(!viz[vecin])
        {
            dfs(vecin, level + 1);
            low[i] = min(low[i], low[vecin]);

            if(low[vecin] >= niv[i])
            {
                sol[++nr].push_back(i); // sol[nr] = componenta biconexa nr
                do
                {
                    x = s.top();
                    s.pop();
                    sol[nr].push_back(x);
                }
                while(x != vecin);
            }
        }
        else low[i] = min(low[i], niv[vecin]);
}

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

    dfs(1, 1);
    g<<nr<<'\n';
    for(i = 1; i <= nr; ++i)
    {
        for(auto j : sol[i]) g<<j<<" ";
        g<<'\n';
    }
    return 0;
}