Cod sursa(job #2859259)

Utilizator rareshinnhoMiroiu Rares rareshinnho Data 1 martie 2022 08:54:11
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 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])
            {
                int X;
                sol[++nr].push_back(i);
                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;
}