Cod sursa(job #2476207)

Utilizator xCata02Catalin Brita xCata02 Data 18 octombrie 2019 12:51:50
Problema Componente biconexe Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>
#define MaxN 100005
using namespace std;

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

#define cin fin
#define cout fout

int n, m, x, y;

vector <int> adj[MaxN];
vector<int > dfn, low;

vector< vector <int> > C;

stack <pair <int, int> > stk;

void read()
{
    cin >> n >> m;
    for(int i=1; i<=m; i++)
    {
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
}

void scoateStiva(int x, int y)
{
    vector <int> con;
    int tx;
    int ty;
    do
    {
        tx = stk.top().first;
        ty = stk.top().second;
        stk.pop();
        con.push_back(tx);
        con.push_back(ty);
    } while(tx != x || ty != y);

    C.push_back(con);
}

void DFS(int n, int fn, int number)
{
    dfn[n] = low[n] = number;
    for(const auto& i: adj[n])
    {
        if(i == fn)
        {
            continue;
        }

        if(dfn[i] == -1)
        {
            stk.push( make_pair(n, i));
            DFS(i, n, number+1);
            low[n] = min(low[n], low[i]);
            if(low[i] >= dfn[n])
            {
                scoateStiva(n, i);
            }
        }
        else
        {
            low[n] = min(low[n], dfn[i]);
        }
    }
}

void solve()
{
    dfn.resize(n+1);
    dfn.assign(n+1, -1);
    low.resize(n+1);

    DFS(1, 0, 0);

    cout << C.size() << endl;
}

int main()
{
    read();
    solve();
}