Cod sursa(job #3211291)

Utilizator stefan05Vasilache Stefan stefan05 Data 8 martie 2024 21:59:36
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
///infoarena Componente tare conexe
#include <fstream>
#include <vector>
#include <algorithm>

#define NMAX 100005

using namespace std;

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

int n, m;
int x, y;
vector<int> l[NMAX];
vector<int> lt[NMAX];
bool f[NMAX];
vector<int> postord;
vector<int> ctc[NMAX]; int nr;
int i, j;

void dfs(int);
void dfst(int);

int main()
{
    fin >>n>>m;
    for (i = 1; i <= m; ++i)
    {
        fin >>x>>y;
        l[x].push_back(y);
        lt[y].push_back(x);
    }

    for (i = 1; i <= n; ++i)
        if (!f[i])
            dfs(i);

    for (i = postord.size()-1; i >= 0; --i)
        if (f[postord[i]])
        {
            nr ++;
            dfst(postord[i]);
        }

    fout <<nr<<'\n';
    for (i = 1; i <= nr; ++i)
    {
        for (auto vf: ctc[i])
            fout <<vf<<' ';
        fout <<'\n';
    }
    fout.close();
    return 0;
}

void dfst(int vf)
{
    f[vf] = 0;
    ctc[nr].push_back(vf);
    for (auto vfnou: lt[vf])
        if (f[vfnou])
            dfst(vfnou);
}

void dfs(int vf)
{
    f[vf] = 1;
    for (auto vfnou: l[vf])
        if (!f[vfnou])
            dfs(vfnou);

    postord.push_back(vf);
}