Cod sursa(job #2723271)

Utilizator beingsebiPopa Sebastian beingsebi Data 13 martie 2021 20:19:54
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("biconex.in");
ofstream g("biconex.out");
// #define f cin
// #define g cout
int n, m;
vector<vector<int>> v, rez;
vector<bool> bif;
vector<int> low, lvl;
stack<int> s;
void dfs(int x, int p)
{
    bif[x] = 1;
    low[x] = lvl[x] = 1 + lvl[p];
    s.push(x);
    for (const int &ac : v[x])
    {
        if (ac == x)
            continue;
        if (bif[ac])
            low[x] = min(low[x], lvl[ac]);
        else
        {
            dfs(ac, x);
            low[x] = min(low[x], low[ac]);
            if (low[ac] >= lvl[x])
            {
                rez.push_back(vector<int>());
                rez.back().push_back(x);
                int ax;
                do
                {
                    ax = s.top();
                    rez.back().push_back(ax);
                    s.pop();
                } while (ax != ac);
            }
        }
    }
}
int main()
{
    f >> n >> m;
    v.resize(n + 2);
    lvl.resize(n + 2);
    low.resize(n + 2);
    bif.resize(n + 2);
    for (int x, y; m; m--)
        f >> x >> y, v[x].emplace_back(y), v[y].emplace_back(x);
    for (int i = 1; i <= n; i++)
        if (!bif[i])
            dfs(i, 0);
    g << rez.size() << '\n';
    for (const auto &i : rez)
    {
        for (const auto &j : i)
            g << j << " ";
        g << '\n';
    }
    return 0;
}