Cod sursa(job #363153)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 11 noiembrie 2009 22:43:11
Problema Componente tare conexe Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<fstream>
#include<iostream>
#define inf "ctc.in"
#define outf "ctc.out"
#define MaxN 10001
using namespace std;

fstream f(inf,ios::in),g(outf,ios::out);

int MA[MaxN][MaxN];
int N,M;
int Pred[MaxN],Suc[MaxN];
int nrc=1;

void DF_Suc(int nod)
{
Suc[nod]=nrc;
for(int i=1;i<=N;i++)
    {
    if(MA[nod][i]==1 && Suc[i]==0)DF_Suc(i);
    }
}

void DF_Pred(int nod)
{
Pred[nod]=nrc;
for(int i=1;i<=N;i++)
    {
    if(MA[i][nod]==1 && Pred[i]==0)DF_Pred(i);
    }
}

void Citire()
{
int i,j;
f>>N>>M;
while(f>>i>>j)
    {
    MA[i][j]=1;
    }
}

int main()
{
Citire();
for(int i=1;i<=N;i++)
    {
    if(Suc[i]==0)
        {
        DF_Suc(i);
        DF_Pred(i);
        for(int j=1;j<=N;j++)
            if(Pred[j]!=Suc[j])Pred[j]=Suc[j]=0;
        nrc++;
        }
    }
g<<nrc-1<<endl;
for(int i=1;i<nrc;i++)
    {
    for(int j=1;j<=N;j++)
        if(Suc[j]==i)g<<j<<" ";
    g<<endl;
    }
f.close();
g.close();
return 0;
}