Cod sursa(job #3042059)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 3 aprilie 2023 21:11:58
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
int n, m, timer, id[N], low[N];
vector <int> g[N];
stack <int> st;
vector <vector <int>> bcc;
void dfs(int u, int p = 0) {
    id[u] = low[u] = id[p] + 1;
    st.push(u);
    for(int v : g[u]) {
        if(id[v]) low[u] = min(low[u], id[v]);
        else {
            dfs(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] >= id[u]) {
                bcc.push_back({u, v});
                while(st.top() != v) {
                    bcc.back().push_back(st.top());
                    st.pop();
                }
                st.pop();
            }
        }
    }
}

int main()
{
#ifndef HOME
    ifstream cin("biconex.in");
    ofstream cout("biconex.out");
#endif
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1, u, v; i <= m; i++) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 1; i <= n; i++) if(!id[i])
        dfs(i);
    cout << bcc.size() << "\n";
    for(auto& c : bcc) {
        for(int x : c)
            cout << x << " ";
        cout << "\n";
    }
    return 0;
}