Cod sursa(job #1250208)

Utilizator AndreiTimofteAndrei Timofte AndreiTimofte Data 27 octombrie 2014 21:40:54
Problema Componente tare conexe Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>
#define NMAX 501

using namespace std;

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

int n, m;
int A[NMAX][NMAX], A_t[NMAX][NMAX], M[NMAX][NMAX];
bool uz[NMAX], uz_t[NMAX], uz_ctc[NMAX];

void citire()
{
    int x, y;

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

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

void DFS_t(int k)
{
    uz_t[k]=1;
    for (int i=1; i<=A_t[k][0]; i++)
        if  (!uz_t[A_t[k][i]])
        DFS_t(A_t[k][i]);
}

void afisare(int nr)
{
    fout<<nr-1<<'\n';

    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=M[i][0]; j++)
            fout<<M[i][j]<<" ";
        fout<<'\n';
    }
}

int main()
{
    citire();

    int ok, lg;
    int nr=1;

    for (int i=1; i<=n; i++)
    {
        ok=0; lg=0;

        DFS(i); DFS_t(i);

        for (int j=1; j<=n; j++)
            if (uz[j] == uz_t[j] && !uz_ctc[j])
            {
                     ok=1;
                     M[nr][++lg]=j;
                     uz_ctc[j] = 1;
            }

        if (ok)
        {
            M[nr][0]=lg;
            nr++;
        }
    }

    afisare(nr);

    return 0;
}