Cod sursa(job #1967122)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 15 aprilie 2017 21:56:23
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <vector>

using namespace std;

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

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

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

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

int main() {
    f >> n >> m;
    for (i = 1; i <= m; i++) {
        f >> x >> y;
        ls[x].push_back(y);
        ls[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0) dfs(i,0);

    g << comp << "\n";
    for (i = 1; i <= comp; i++) {
        for (j = 0; j < sol[i].size(); j++)
            g << sol[i][j] << ' ';
        g << '\n';
    }
    return 0;
}