Cod sursa(job #2137131)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 20 februarie 2018 17:08:22
Problema Componente tare conexe Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector <int> L[100002], G[100002], sol[100002];
int k, n, m, c, viz[100002], sir[100002];

void DFSt(int nod)
{
    viz[nod] = 1;

    for(auto i : L[nod])
        if(!viz[i])
            DFSt(i);

    sir[++k] = nod;
}

void DFSg(int nod)
{
    viz[nod] = 1;

    for(auto i : G[nod])
        if(!viz[i])
            DFSg(i);

    sol[c].push_back(nod);
}
void Citire()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        G[y].push_back(x);
    }

    for(int i = 1; i <= n; i++)
        if(!viz[i])
            DFSt(i);

    for(int i = 1; i <= n; i++)
        viz[i] = 0;

    for(int i = k; i >= 1; i--)
        if(!viz[sir[i]])
            ++c,
            DFSg(sir[i]);

    fout << c << "\n";
    for(int i = 1; i <= c; i++){
        for(auto j : sol[i])
            fout << j << " ";
        fout << "\n";
    }
}
int main()
{
    Citire();
    return 0;
}