Cod sursa(job #2084340)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 8 decembrie 2017 23:02:17
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("biconex.in");
ofstream g("biconex.out");

const int nmax = 100005;
vector<int> ls[nmax];
int viz[nmax], lmax[nmax], niv[nmax], stiva[nmax], vf, i, j, n, m, x, y, s2, l;
vector<vector<int> >sol;

void baga(int x, int tt) {
    vector <int> ss;
    ss.push_back(tt);
    while (stiva[vf]!=x && vf > 0)
        ss.push_back(stiva[vf--]);
    if (vf > 0) {
        ss.push_back(x);
        vf--;
    }
    sol.push_back(ss);
}

void dfs(int x, int tt) {
    int l = ls[x].size(), i, y;
    viz[x] = 1;
    lmax[x] = niv[x] = niv[tt]+1;
    for (i = 0; i < l; i++) {
        y = ls[x][i];
        if (viz[y]) {
            if (y != tt)
                lmax[x] = min(lmax[x], niv[y]);
        } else {
            stiva[++vf] = y;
            dfs(y,x);
            lmax[x] = min(lmax[x], lmax[y]);
            if (lmax[y] >= niv[x])
                baga(y,x);
        }
    }
}

int main() {
    f >> n >> m;
    while (m--) {
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
    }
    dfs(1,0);
    g << (s2=sol.size()) << '\n';
    for (i = 0; i < s2; i++) {
        l = sol[i].size();
        for (j = 0; j < l; j++)
            g << sol[i][j] <<' ';
        g << '\n';
    }
}