Cod sursa(job #2404918)

Utilizator alexilasiAlex Ilasi alexilasi Data 13 aprilie 2019 16:16:41
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nMax = 100010;

int n, m, h, a;
int low[nMax], height[nMax], st[nMax];
vector <int> v[nMax], ans[nMax];

void dfs(int nod)
{
    st[++h] = nod;
    for(auto it : v[nod])
    {
        if(!height[it])
        {
            int x = h;
            height[it] = low[it] = height[nod] + 1;
            dfs(it);
            low[nod] = min(low[nod], low[it]);

            if(low[it] >= height[nod])
            {
                ans[++a].push_back(nod);
                while(h > x)
                    ans[a].push_back(st[h--]);
            }
        }
        low[nod] = min(low[nod], height[it]);
    }
}

int main()
{
    fin >> n >> m;
    for(int i=1, x, y; i<=m; i++)
    {
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    low[1] = height[1] = 1;
    dfs(1);
    fout << a << '\n';
    for(int i=1; i<=a; i++)
    {
        for(auto it : ans[i])
            fout << it << " ";
        fout << '\n';
    }
}