Cod sursa(job #1972023)

Utilizator gundorfMoldovan George gundorf Data 21 aprilie 2017 15:23:34
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <iostream>
#include <fstream>
#define Nmax  100301
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n,m;
struct nod
{
    int info;
    nod *urm;
};
nod *gf[Nmax];//graful orientat
nod *gft[Nmax];//graful orientat transpus
nod *sol[Nmax];//cate o componenta tare conexa in fiecare lista
int nrCTC;//numarul de componente tare conexe
void ADD (nod *&prim,int x)
{
    nod *q=new nod;
    q->info=x;
    q->urm=prim;
    prim=q;
}
void Citire()
{
    int i;
    int x,y;
    fin>>n>>m;
    for (i=1; i<=m; i++)
    {
        fin>>x>>y;
        ADD(gf[x],y);
        ADD(gft[y],x);
    }

}
void DFS (int viz[Nmax],int x)
{
    viz[x]=1;
    nod *p;
    for (p=gf[x]; p!=NULL; p=p->urm)
        if (viz[p->info]==0)DFS(viz,p->info);

}
void DFST (int viz[Nmax],int x)
{
    viz[x]=1;
    nod *p;
    for (p=gft[x]; p!=NULL; p=p->urm)
        if (viz[p->info]==0)DFST(viz,p->info);

}
void Ctc (int x)
{
    int viz1[Nmax]={0},viz2[Nmax]= {0},i;
    nrCTC++;
    DFS(viz1,x);
    DFST(viz2,x);
    for (i=1; i<=n; i++)
        if (viz1[i]==1&&viz2[i]==1)
        {   ADD(sol[nrCTC],i);
            gf[i]->urm=NULL;
            //gft[i]->urm=NULL;
        }
}

void Rezolva ()
{
    int i,ok=1;
    while (ok!=0)
    {
        ok=0;
        for (i=1; i<=n; i++)
            if (gf[i]!=NULL)
            if (gf[i]->urm!=NULL)
            {
                Ctc(i);
                ok=1;
            }
    }
}
int main()
{
    int i;
    nod *p;
    Citire();
    for (i=1;i<=n;i++)
        if (gf[i]==NULL)
    {
        nrCTC++;
        ADD(sol[nrCTC],i);
    }
    Rezolva();

    fout<<nrCTC<<"\n";
    for (i=1; i<=nrCTC; i++)
    {
        for (p=sol[i]; p!=NULL; p=p->urm)
            fout<<p->info<<" ";
        fout<<"\n";
    }
    return 0;
}