Cod sursa(job #2971925)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 28 ianuarie 2023 12:52:13
Problema Componente tare conexe Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int max_size = 1e5 + 1;

vector <int> mc[max_size], invmc[max_size], topsort, ctc;
vector <vector <int>> rasp;
int viz[max_size];

void dfs (int nod)
{
    viz[nod] = 1;
    for (auto f : mc[nod])
    {
        if (!viz[f])
        {
            dfs(f);
        }
    }
    topsort.push_back(nod);
}

void kos (int nod)
{
    viz[nod] = 1;
    ctc.push_back(nod);
    for (auto f : mc[nod])
    {
        if (!viz[f])
        {
            kos(f);
        }
    }
}

int main ()
{
    int n, m;
    in >> n >> m;
    while (m--)
    {
        int x, y;
        in >> x >> y;
        mc[x].push_back(y);
        invmc[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            dfs(i);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        viz[i] = 0;
    }
    int ct = 0;
    for (auto f : topsort)
    {
        if (!viz[f])
        {
            ct++;
            kos(f);
            rasp.push_back(ctc);
            ctc.clear();
        }
    }
    out << ct << '\n';
    for (auto f : rasp)
    {
        for (auto ff : f)
        {
            out << ff << " ";
        }
        out << '\n';
    }
    in.close();
    out.close();
    return 0;
}