Cod sursa(job #2739829)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 10 aprilie 2021 11:40:25
Problema Componente biconexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = (1 << 30), NMAX(100005);
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;

void BUNA(const string& task = "")
{
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
                freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PA()
{
    exit(0);
}
VI G[NMAX], rez[NMAX];
int low[NMAX], dfn[NMAX], nr;
stack<int> st;

void AddComp(int nod){
    ++nr;
    while(st.top() != nod){
        rez[nr].push_back(st.top());
        st.pop();
    }
    rez[nr].push_back(st.top());
    st.pop();
}

void BICONEX(int nod, int niv){
    dfn[nod] = low[nod] = niv;
    st.push(nod);
    for(auto it: G[nod]){
        if(dfn[it] == -1){
            BICONEX(it, niv + 1);
            low[nod] = min(low[nod], low[it]);
            if(low[it] >= dfn[nod]){
                if(st.size() > 0)
                    AddComp(it);
                rez[nr].push_back(nod);
            }
        }
        else low[nod] = min(low[nod], dfn[it]);
    }
}

int main()
{
    BUNA();
    int n, m;
    cin >> n >> m;

    for(int i = 1; i <= m; ++i){
        int x, y;
        cin >> x >> y;

        G[x].push_back(y);
        G[y].push_back(x);
    }

    for(int i = 1; i <= n; ++i)
        low[i] = dfn[i] = -1;

    BICONEX(1, 1);

    cout << nr << '\n';
    for(int i = 1; i <= nr; ++i){
        for(auto it: rez[i])
            cout << it << ' ';
        cout << '\n';
    }
    PA();
}