Pagini recente » Cod sursa (job #2341745) | Cod sursa (job #47067) | Cod sursa (job #911941) | Cod sursa (job #487840) | Cod sursa (job #247472)
Cod sursa(job #247472)
#include <stdio.h>
struct elem
{
int nod;
elem *next;
};
elem *p[100];
int vizitat_df[100];
void adauga(int j,int k)
{
elem *newel=new elem;
newel->nod=k;
if (p[j]==NULL)
{
p[j]=newel;
newel->next=NULL;
}
else
{
newel->next=p[j];
p[j]=newel;
}
}
void DF(int x)
{
elem *current=p[x];
while(current!=NULL)
{
if(vizitat_df[current->nod]==0)
{
vizitat_df[current->nod]=1;
DF(current->nod);
}
current=current->next;
}
}
int main()
{
int n,m,i,x,y,nr=0;
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
adauga(x,y);
}
for(i=1;i<=n;++i)
{
if(vizitat_df[i]==0)
{
nr++;
vizitat_df[i]=1;
DF(i);
}
}
printf("%d",nr);
return 0;
}