Cod sursa(job #2427953)

Utilizator FlaviusFeteanFetean Flavius FlaviusFetean Data 3 iunie 2019 09:22:41
Problema Componente biconexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <stack>

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

int d[100005], v[100005], f[100005];
bool s[100005], a[100005];

stack<int> G[100005];
stack<int> st;

int main()
{
    int n, m, i, j, x, y, ctcb = 1;

    fin >> n >> m;
    for(i = 1; i <= m; i++){
        fin >> x >> y;
        G[x].push(y); G[y].push(x);
    }

    st.push(1);
    s[1] = d[1] = 1;

    while(!st.empty()){
        x = st.top();
        y = G[x].top(); G[x].pop();
        while(!G[x].empty()){
            if(s[y]){
                    if(v[x] == 0) v[x] = d[y];
                    if(v[x] > d[y]) v[x] = d[y];
            }
            else{
                d[y] = d[x] + 1;
                st.push(y); f[y] = x; s[y] = 1;
                break;
            }
            y = G[x].top(); G[x].pop();
        }
        if(G[x].empty()) st.pop();
    }

    for(i = n; i > 1; i--){
        if(v[f[i]] > v[i]) v[f[i]] = v[i];
    }

    for(i = n; i > 2; i--){
        if(d[f[i]] <= v[i]) a[f[i]] = 1, ctcb++;
    }

    fout << ctcb << "\n";



    return 0;
}