Cod sursa(job #2541107)

Utilizator MarcGrecMarc Grec MarcGrec Data 8 februarie 2020 09:35:51
Problema Componente biconexe Scor 46
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#define MAX_N 100000

#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <bitset>
using namespace std;

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

int n, m, c, t, T[MAX_N + 1];
vector<int> G[MAX_N + 1], C[MAX_N + 1];
stack<int> N;

int Df(int nod);

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

    for (int i = 1; i <= n; ++i)
    {
        if (T[i] == 0)
        {
            Df(i);
        }
    }

    fout << c << '\n';
    for (int i = 0; i < c; ++i)
    {
        for (int nod : C[i])
        {
            fout << nod << ' ';
        }
        fout << '\n';
    }

    fin.close();
    fout.close();
    return 0;
}

int Df(int nod)
{
    int res = T[nod] = ++t;
    N.push(nod);
    for (int vecin : G[nod])
    {
        if (T[vecin] == 0)
        {
            int resv = Df(vecin);
            res = min(res, resv);

            if (resv >= T[nod])
            {
                while (N.top() != nod)
                {
                    C[c].push_back(N.top());
                    N.pop();
                }
                C[c++].push_back(N.top());
            }
        }
        else
        {
            if (T[vecin] != (T[nod] - 1))
            {
                res = min(res, T[vecin]);
            }
        }
    }

    return res;
}