Cod sursa(job #2594484)

Utilizator nTropicManescu Bogdan Constantin nTropic Data 6 aprilie 2020 02:09:25
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>

using namespace std;

const int len = 100005;
int m, n, x, y, cnt, aux, depth[len], low[len];
vector<bool> seen(len, false);
vector<int> g[len], sol[len];
stack<int> s;

void dfs(int node, int parent = 0) {
    low[node] = depth[node] = depth[parent] + 1;
    seen[node] = true;
    s.push(node);
    for (int& next : g[node])
        if (next != parent) {
            if (seen[next])
                low[node] = min(low[node], depth[next]);
            else {
                dfs(next, node);
                low[node] = min(low[node], low[next]);
                if (low[next] >= depth[node]) {
                    do {
                        aux = s.top();
                        sol[cnt].push_back(aux);
                        s.pop();
                    } while (aux != next);
                    sol[cnt++].push_back(node);
                }
            }
        }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    freopen("biconex.in", "r", stdin);
    freopen("biconex.out", "w", stdout);

    cin >> n >> m;
    while (m--) {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for (int i = 0; i < n; i++)
        if (!seen[i])
            dfs(i);

    cout << cnt << "\n";
    for (int i = 0; i < cnt; i++) {
        for (int& it : sol[i])
            cout << it << " ";
        cout << "\n";
    }
}