Cod sursa(job #1251333)

Utilizator DavidGhGhorbani David DavidGh Data 29 octombrie 2014 12:11:01
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>
#define NMAX 100001

using namespace std;

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

vector <int> A[NMAX];
vector <int>A_t[NMAX];
vector <int>sol[NMAX];

int uz[NMAX], uz_t[NMAX], post[NMAX];
int n, m;
int nrc, lg;

void citire()
{
    int x, y;

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

void DFS(int k)
{
    uz[k]=1;
    for (int i=0; i<A[k].size(); i++)
        if (!uz[A[k][i]])
            DFS(A[k][i]);

    post[++lg]=k;
}

void DFS_t(int k)
{
    uz_t[k]=1;
    sol[nrc].push_back(k);
    for (int i=0; i<GT[k].size(); i++)
        if (!uz_t[GT[k][i]])
            DFS_t(GT[k][i]);

}

void afisare()
{
    fout<<nrc<<'\n';
    for (int i=1; i<=nrc; i++)
    {
        for (int j=0; j<sol[i].size(); j++)
            fout<<sol[i][j]<<' ';
        fout<<'\n';
    }
}

int main()
{
    citire();

    for (int i=1; i<=n; ++i)
        if (!uz[i])
            DFS(i);

    for (int i=n; i>=1; i--)
        if (!uz_t[post[i]])
        {
            nrc++;
            DFS_t(post[i]);
        }

    afisare();

    return 0;
}