Cod sursa(job #2870987)

Utilizator domistnSatnoianu Dominic Ioan domistn Data 12 martie 2022 19:26:26
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <iostream>
#include <vector>
#include <stack>

#define NMAX 100005

using namespace std;

int n, m, nrm[NMAX], niv[NMAX], ansl;
bool v[NMAX];
vector<int> adj[NMAX], ans[NMAX];
stack<int> st;

inline void dfs(int nod, int tata = 0) {
    v[nod] = 1;
    niv[nod] = niv[tata] + 1;
    nrm[nod] = niv[nod];
    st.push(nod);
    for(const auto &el : adj[nod]) {
        if(el == tata) continue;
        if(v[el]) {
            nrm[nod] = min(nrm[nod], niv[el]);
        } else {
            dfs(el, nod);
            nrm[nod] = min(nrm[nod], nrm[el]);
            if(nrm[el] >= niv[nod]) {
                ++ansl;
                while(st.top() != el) {
                    ans[ansl].push_back(st.top());
                    st.pop();
                }
                ans[ansl].push_back(el);
                st.pop();
                ans[ansl].push_back(nod);
            }
        }
    }
}

int main()
{
    freopen("biconex.in", "r", stdin);
    freopen("biconex.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1, x, y; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dfs(1);
    printf("%d\n", ansl);
    for(int i = 1; i <= ansl; ++i) {
        for(const auto &el : ans[i])
            printf("%d ", el);
        printf("\n");
    }
    return 0;
}