Cod sursa(job #307173)

Utilizator DraStiKDragos Oprica DraStiK Data 23 aprilie 2009 16:58:51
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <stdio.h>
#define DIM 100005
struct nod {int x;
            nod *urm;} *lst[DIM];
int viz[DIM],c[DIM];
int n,m,nrt;
void add (int a,int b)
{
    nod *p=new nod;
    p->x=b;
    p->urm=lst[a];
    lst[a]=p;
}
void df (int val)
{
    nod *p;
	viz[val]=1;
    for (p=lst[val]; p; p=p->urm)
        if (!viz[p->x])
            df (p->x);
}

int main ()
{
    freopen ("dfs.in","r",stdin);
    freopen ("dfs.out","w",stdout);
    int i,x,y;
    scanf ("%d%d",&n,&m);
    for (i=1; i<=m; ++i)
    {
        scanf ("%d%d",&x,&y);
		add (x,y);
        add (y,x);
    }
    for (i=1; i<=n; ++i)
		if (!viz[i])
		{
			df (i);
			++nrt;
        }
    printf ("%d",nrt);
    return 0;
}