Cod sursa(job #2580863)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 14 martie 2020 11:54:44
Problema Componente biconexe Scor 36
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <stack>
#include <iostream>
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 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;
                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 != x || curr.second != next);
                bi.push_back(vec);
            }
            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) {
        for(auto x: a)
            fout << x <<  ' ';
        fout << '\n';
    }
}

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