Cod sursa(job #2923796)

Utilizator PetstebPopa Petru Petsteb Data 19 septembrie 2022 11:25:28
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream in("ctc.in");
ofstream out("ctc.out");

const int NMAX = 1e5 + 5;
int n, m, depth[NMAX], low_link[NMAX];
vector<int> G[NMAX], stiva, comp;
vector<vector<int>> ctc;
bool used[NMAX];

void dfs(int nod, int d)
{
    used[nod] = 1;
    stiva.push_back(nod);
    depth[nod] = d;
    low_link[nod] = depth[nod];
    for(auto vecin : G[nod])
    {
        if(!used[vecin])
            dfs(vecin, d + 1);
        low_link[nod] = min(low_link[nod], low_link[vecin]);
    }
    if(low_link[nod] == depth[nod])
    {
        comp.clear();
        while(stiva.back() != nod)
        {
            comp.push_back(stiva.back());
            stiva.pop_back();
        }
        stiva.pop_back();
        comp.push_back(nod);
        ctc.push_back(comp);
    }
}

int main()
{
    in >> n >> m;
    while(m--)
    {
        int x, y;
        in >> x >> y;
        G[x].push_back(y);
    }
    dfs(1, 1);
    out << ctc.size() << '\n';
    for(auto c : ctc)
    {
        for(int elem : c)
            out << elem << ' ';
        out << '\n';
    }
    return 0;
}

///Tarjan
///solutie Toma Ariciu