Cod sursa(job #2870745)

Utilizator servus2022Stefan Tonut servus2022 Data 12 martie 2022 15:39:21
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.92 kb
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

ifstream f("biconex.in");
ofstream g("biconex.out");

const int NMAX = 1e5 + 1;
const int ROOT = 1;

int N;
vector < int > G[NMAX];

bool Sel[NMAX];
int Level[NMAX], Low[NMAX];

stack < int > St;

vector < vector < int > > Sol;

static inline void Read ()
{
    f.tie(nullptr);

    f >> N;

    int M = 0;
    f >> M;

    for(int i = 1; i <= M; ++i)
    {
        int u = 0, v = 0;
        f >> u >> v;

        G[u].push_back(v), G[v].push_back(u);
    }

    return;
}

static inline int my_min (int a, int b)
{
    return ((a < b) ? a : b);
}

static inline void DFS (int Node, int from = 0)
{
    Sel[Node] = 1;

    Level[Node] = Low[Node] = (Level[from] + 1);

    St.push(Node);

    for(auto it : G[Node])
        if(!Sel[it])
        {
            DFS(it, Node);

            Low[Node] = my_min(Low[Node], Low[it]);

            if(Low[it] >= Level[Node])
            {
                vector < int > _temp;
                int X = 0;

                do
                {
                    X = St.top(), _temp.push_back(X), St.pop();
                }
                while(X != it);

                _temp.push_back(Node);

                Sol.push_back(_temp);
            }
        }
        else
            Low[Node] = my_min(Low[Node], Level[it]);

    return;
}

static inline void Write ()
{
    g << (int)Sol.size() << '\n';

    for(auto i : Sol)
    {
        sort(i.begin(), i.end());

        for(int j = 0; j < (int)i.size(); ++j)
        {
            g << i[j];

            if(j != (int)i.size() - 1)
                g << ' ';
        }

        g << '\n';
    }

    return;
}

static inline void Solve ()
{
    DFS(ROOT);

    Write();

    return;
}

int main ()
{
    Read();

    Solve();

    return 0;
}