Cod sursa(job #2723444)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 14 martie 2021 01:59:23
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("biconex.in");
ofstream fout ("biconex.out");

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 1e5 + 5;

vector < int > a[N], add;
vector < vector < int > > sol;
stack < pair < int, int > > st;

int disc[N], low[N], temp;
void found(int x, int y)
{
    vector < int > add;
    int topf = st.top().first, tops = st.top().second;
    do {
        topf = st.top().first, tops = st.top().second;
        add.push_back(topf);
        add.push_back(tops);
        st.pop();
    }
    while(topf != x && tops != y);
    sol.push_back(add);
}

void dfs(int k)
{
    disc[k] = low[k] = ++temp;
    for(auto v : a[k]) {
        if(disc[v] == 0) {
            st.push({k, v});
            dfs(v);
            low[k] = min(low[k], low[v]);
            if(low[v] >= disc[k]) {
                found(k, v);
            }
        }
        else {
            low[k] = min(low[k], disc[v]);
        }
    }
}
int main()
{
    usain_bolt();

    int n, m;

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

        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i) {
        if(disc[i] == false) {
            dfs(i);
        }
    }
    fout << (int) sol.size() << "\n";
    for(auto v : sol) {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        for(auto w : v) {
            fout << w << " ";
        }
        fout << "\n";
    }
    return 0;
}