Cod sursa(job #2334019)

Utilizator catalina200029Olteanu Catalina catalina200029 Data 2 februarie 2019 10:29:02
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>

using namespace std;

struct nod{
    int inf;
    nod *urm;
}*l[100010],*l2[100010],*r[100010];

int n,nr,sol[100010],viz[100010],m,t;

void add(nod *&p,int x){
    nod *aux;
    aux = new nod;
    aux->urm=p;
    aux->inf=x;
    p=aux;
}

void df(int x){
    nod *i;
    viz[x]=1;
    for (i=l[x];i!=NULL;i=i->urm){
        if (!viz[i->inf]){
            df(i->inf);
        }

    }
    sol[--nr]=x;
}

void df2(int x){
    nod *i;
    viz[x]=1;
    add(r[t],x);
    for (i=l2[x];i!=NULL;i=i->urm){
        if (!viz[i->inf]){
            df2(i->inf);
        }

    }
}

int main()
{
    int i,x,y;
    freopen("ctc.in","r",stdin);
    freopen("ctc.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        add(l[x],y);
        add(l2[y],x);
    }
    nr=n+1;
    for (i=1;i<=n;i++ )
        if (!viz[i]) df(i);
    t=0;
    memset(viz,0,sizeof(viz));
    for (i=1;i<=n;i++)
        if (!viz[sol[i]]) {
            t++;
            df2(sol[i]);
        }
    printf("%d\n",t);
    for (i=1;i<=t;i++){
        nod *j;
        for (j=r[i];j!=NULL;j=j->urm)
            printf("%d ",j->inf);
        printf("\n");
    }
    return 0;
}