Cod sursa(job #3030855)

Utilizator Cosmin2004_InfoMoldoveanu Cosmin Cosmin2004_Info Data 17 martie 2023 22:22:08
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
vector <int> g[N];
int id[N], low[N], t;
stack <int> st;
vector <vector <int>> bcc;
void dfs(int u, int p) {
    id[u] = low[u] = 1 + id[p];
    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");
#else
    ios_base :: sync_with_stdio(false); cin.tie(0);
#endif
    int n, m;
    cin >> n >> m;
    for(int i = 0, 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, 0);
    cout << bcc.size() << "\n";
    for(auto& c : bcc) {
        for(int x : c)
            cout << x << " ";
        cout << "\n";
    }
    return 0;
}