Cod sursa(job #1839213)

Utilizator valentinoMoldovan Rares valentino Data 2 ianuarie 2017 16:40:19
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#define NM 100005
#include <vector>
using namespace std;

ifstream f("ctc.in");
ofstream g("ctc.out");

vector < int > GT[NM], Gsol[NM], G[NM];
int n, m, viz[NM], where[NM], postordine[NM], nr, sol;

void Read()
{
    int x, y;
    f >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        f >> x >> y;
        G[x].push_back(y);
        GT[y].push_back(x);
    }
}

void DFST(int x)
{
    where[x] = sol;
    for(int  i = 0 ; i < GT[x].size(); ++i)
    {
        if(!where[ GT[x][i] ])
            DFST( GT[x][i] );
    }
}

void DFS(int x)
{
    viz[x] = 1;
    for(int i = 0 ; i < G[x].size(); ++i)
    {
        if(viz[G[x][i]] == 0) DFS(G[x][i]);
    }
    postordine[++nr] = x;
}

int main()
{
    Read();
    for(int i = 1; i <= n; ++i)
    if(!viz[i]) DFS(i);
    for(int i = n; i > 0; --i)
    {
        if( !where[ postordine[ i ] ] )
        {
            sol++;
            DFST(postordine[i]);

        }
    }
    for(int i = 1; i <= n; ++i)
        Gsol[where[i]].push_back(i);
    g << sol << '\n';
    for(int i = 1; i <= sol; ++i)
    {
        for(int j = 0; j < Gsol[i].size(); ++j)
            g << Gsol[i][j] << ' ';
        g << '\n';
    }
}