Cod sursa(job #2198107)

Utilizator EclipseTepes Alexandru Eclipse Data 23 aprilie 2018 17:37:21
Problema Componente biconexe Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#define dMAX 100000

using namespace std;

int n, m, x, y, c, cnt;
vector<int> graf[dMAX + 2];

int nChildren, start;

stack<pair<int, int> > myStack;
int dfn[dMAX + 2], low[dMAX + 2], art[dMAX + 2];

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

void Biconex(int v, int pv) {
    int newV, u;
    dfn[v] = low[v] = ++cnt;
    for (u = 0; u < graf[v].size(); u++) {
        newV = graf[v][u];
        if (newV != pv && dfn[newV] < dfn[v]) {
            myStack.push({newV, v});
        }
        if (!dfn[newV]) {
            if (v == start) nChildren++;
            Biconex(newV, v);
            low[v] = min(low[v], low[newV]);
            if (low[newV] >= dfn[v]) {
                if (v != start) art[v] = 1;
                /// Afisare
                c++;
            }
        } else {
            if (newV != pv) {
                low[v] = min(low[v], dfn[newV]);
            }
        }
    }
}

int main()
{
    int i, j;
    fin >> n >> m;
    for (i = 1; i <= m; i++) {
        fin >> x >> y;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }

    start = 1;

    Biconex(start, -1);

    fout << c;

    return 0;
}