Cod sursa(job #632090)

Utilizator Sm3USmeu Rares Sm3U Data 10 noiembrie 2011 12:02:01
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <cstdio>
#define nMax 100100

using namespace std;

int n;
int m;
int viz[nMax];
int s;

struct nod{
    int x;
    nod *urm;
}*a[nMax];

void citire()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x;
        int y;
        scanf("%d%d",&x,&y);
        nod *z;
        z = new nod;
        z -> x = y;
        z -> urm = a[x];
        a[x] = z;
    }
}


void fill(int k)
{
    for(nod *i=a[k];i;i=i->urm)
    {
        if( !viz[i -> x] )
        {
            viz[i -> x] = 1;
            fill( i -> x );
        }
    }
}

void rez()
{
    for(int i=1;i<=n;i++)
    {
        if(!viz[i])
        {
            viz[ i ] = 1;
            s++;
            fill(i);
        }
    }
    printf("%d\n", s);
}

int main()
{
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    citire();
    rez();
    return 0;
}