Cod sursa(job #2580865)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 14 martie 2020 11:58:26
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;

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

int n, m, nr;
int art[100005];
vector<int> pctArt;
int nivel[100005], low[100005];
vector<int> g[100005];
bool v[100005];
stack<pair<int, int> > st;
vector<vector<int> > bi;

void citire() {
    fin >> n >> m;
    while(m--) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

void add(int a, int b) {
    pair<int, int> curr;
    vector<int> vec;
    do {
        curr = st.top();
        vec.push_back(curr.first);
        vec.push_back(curr.second);
        st.pop();
    } while(curr.first != a || curr.second != b);
    bi.push_back(vec);


}

void dfs(int x, int t) {
    v[x] = 1;
    int nrcopii = 0;
    nivel[x] = nivel[t]+1;
    low[x] = nivel[x];

    for(auto next: g[x]) {
        if(!v[next]) {
            nrcopii++;
            st.push({x, next});
            dfs(next, x);
            if(low[next] >= nivel[x]) {
                art[x] = true;
                add(x, next);
            }
            low[x] = min(low[x], low[next]);
        } else if(next != t)
            low[x] = min(low[x], nivel[next]);
    }
}


void solve() {
    for(int i = 1; i <= n; i++)
        if(!v[i])
            dfs(i, 0);
    fout << bi.size() << '\n';
    for(auto a: bi) {
        sort(a.begin(), a.end());
        for(int i = 0; i < a.size(); i++)
            if(a[i] != a[i-1] || i == 0)
                fout << a[i] <<  ' ';
        fout << '\n';
    }
}

int main() {
    citire();
    solve();
}