Cod sursa(job #2954600)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 14 decembrie 2022 21:35:50
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <vector>
#include <stack>

using namespace std;
const int nmax = 1e5 + 9;
vector<int> adj[nmax];
int lvl[nmax];
bool viz[nmax];
stack<int> st;
vector<vector<int>> bcc;

int dfs (int nod = 1)
{
    viz[nod] = 1;
    int reach = 1e9;
    st.push(nod);
    for (auto to : adj[nod])
    {
        if (viz[to])
        {
            reach = min(lvl[to] , reach);
            continue;
        }
        lvl[to] = lvl[nod]+1;
        
        int check = dfs(to);
        reach = min(reach , check);
        if (check >= lvl[nod])
        {
            bcc.push_back({nod});
            while (!st.empty())
            {
                bcc.back().push_back(st.top());
                st.pop();
                if (bcc.back().back() == to) break;
            }
        }
    }
    
    return reach;
}

int main()
{
    freopen("biconex.in" , "r" , stdin);
    freopen("biconex.out" , "w" , stdout);
    int n, m; cin >> n >> m;
    for (int i = 1 ; i <= m ; i++)
    {
        int x, y; cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dfs();
    cout << bcc.size() << '\n';
    for (auto comp : bcc)
    {
        
        for (auto nod : comp) cout << nod << ' ';
        cout << '\n';
    }

}