Cod sursa(job #2940774)

Utilizator brianna_enacheEnache Brianna brianna_enache Data 16 noiembrie 2022 13:30:57
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, nrcc, st[100005], top;
vector <int> a[100005], t[100005];
bitset <100005> viz;
vector <int> ctc[100005];
void DFS(int nod)
{
    viz[nod]=1;
    for(auto i:a[nod])
        if(viz[i]==0) DFS(i);
    st[++top]=nod;
}

void DFS2(int nod)
{
    viz[nod]=0;
    for(auto i : t[nod])
        if(viz[i] == 1) DFS2(i);
    ctc[nrcc].push_back(nod);
}
int main()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        t[y].push_back(x);
    }

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

    for (i = n; i >= 1; i--)
        if (viz[st[i]] == 1)
        {
            nrcc++;
            DFS2(st[i]);
        }
    fout << nrcc << "\n";
    for (i = 1; i <= nrcc; i++)
    {
        for (auto w : ctc[i])
            fout << w << " ";
        fout << "\n";
    }



    return 0;
}