Cod sursa(job #2148273)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 1 martie 2018 17:02:32
Problema Componente tare conexe Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>
#define nmax 100007

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

int n, st[nmax], top, nrc;
vector <int> L[nmax];
vector <int> T[nmax];
vector <int> Sol[nmax];
bool viz[nmax];

void Citire()
{
    int t, x, y;
    fin >> n >> t;
    while (t--)
    {
        fin >> x >> y;
        L[x].push_back(y);
        T[y].push_back(x);
    }
}

void DFS(int nod)
{
    viz[nod] = 1;
    for (auto i : L[nod])
        if (!viz[i])
            DFS(i);
    st[++top] = nod;
}

void DFS2(int nod)
{
    viz[nod] = 0;
    for (auto i : T[nod])
        if (viz[i])
            DFS2(i);
    Sol[nrc].push_back(nod);
}

void Sortare()
{
    int i;
    for (i = 1; i <= nrc; i++)
        sort (Sol[i].begin(), Sol[i].end());
}

void Rezolvare()
{
    int i, nod;

    for (i = 1; i <= n; i++)
        if (!viz[i])
            DFS(i);
    while (top > 0)
    {
        nod = st[top--];
        if (viz[nod])
        {
            nrc++;
            DFS2(nod);
        }
    }
}

void Afisare()
{
    int i;
    fout << nrc << "\n";
    for (i = 1; i <= nrc; i++)
    {
        for (auto j : Sol[i])
            fout << j << " ";
        fout << "\n";
    }
}

int main()
{
    Citire();
    Rezolvare();
    ///Sortare();
    Afisare();
    fin.close();
    fout.close();
    return 0;
}