Cod sursa(job #2215471)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 22 iunie 2018 12:27:30
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>
#include <vector>
#define nmax 100005

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

int n, m, i, j, x, y, niv[nmax], lmax[nmax],stk[nmax], vf, comp;
bool seen[nmax];
vector<int>v[nmax], sol[nmax];

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

void dfs( int x, int k)
{
    int l = v[x].size(), y, i;
    lmax[x] = niv[x] = niv[k]+1;
    seen [x] = 1;
    for ( i = 0; i < l; i++)
        {
            y = v[x][i];
            if (y == k)
                continue;
            if ( seen [y] == 1)
            {
                lmax[x] = min(lmax[x], niv[y]);
                continue;
            }
            stk[++vf] = y;
            dfs( y , x );
            lmax[x] = min(lmax[x], lmax[y]);
            if (niv[x] <= lmax[y])
            {
                comp++;
                get_component(y, x);
            }
        }
}

int main() {
    f >> n >> m;
    for (i = 1; i <= m; i++)
    {
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for ( i = 1; i <= n; i++)
        if (seen [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;
}