Cod sursa(job #2082464)

Utilizator vladvlad00Vlad Teodorescu vladvlad00 Data 6 decembrie 2017 11:45:02
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <vector>

using namespace std;

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

void citire();
void DFS(int x);
void DFST(int x);

int n, m, nr, nrc, uz[100005], postordine[100005];
vector<int> a[100005], b[100005], sol[100005];

int main()
{
    int i, j;

    citire();
    for (i=1;i<=n;i++)
        if (!uz[i])
            DFS(i);
    for (i=n;i>=1;i--)
        if (uz[postordine[i]])
        {
            nrc++;
            DFST(postordine[i]);
        }
    fout << nrc << '\n';
    for (i=1;i<=nrc;i++)
    {
        for (j=0;j<sol[i].size();j++)
            fout << sol[i][j] << ' ';
        fout << '\n';
    }
    return 0;
}

void citire()
{
    int i, x, y;

    fin >> n >> m;
    for (i=1;i<=n;i++)
    {
        a[i].push_back(0);
        b[i].push_back(0);
    }
    for (i=1;i<=m;i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
        a[x][0]++;
        b[y].push_back(x);
        b[y][0]++;
    }
}

void DFS(int x)
{
    int i;

    uz[x] = 1;
    for (i=1;i<=a[x][0];i++)
        if (!uz[a[x][i]])
            DFS(a[x][i]);
    postordine[++nr] = x;
}

void DFST(int x)
{
    int i;

    uz[x] = 0;
    sol[nrc].push_back(x);
    for (i=1;i<=b[x][0];i++)
        if (uz[b[x][i]])
            DFST(b[x][i]);
}