Pagini recente » Cod sursa (job #2790239) | Cod sursa (job #2837843) | Cod sursa (job #1870890) | Cod sursa (job #562718) | Cod sursa (job #203346)
Cod sursa(job #203346)
#include<stdio.h>
#include<stdlib.h>
int n,m;
int *a[100001],cati[100001],rez;
bool ok[100001];
void citire()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
cati[x]++;
a[x]=(int*)realloc(a[x],(cati[x]+1)*sizeof(int));
a[x][cati[x]]=y;
cati[y]++;
a[y]=(int*)realloc(a[y],(cati[y]+1)*sizeof(int));
a[y][cati[y]]=x;
}
}
void dfs(int nod)
{
ok[nod]=true;
for(int i=1; i<=cati[nod]; i++)
{
if(!ok[a[nod][i]])
dfs(a[nod][i]);
}
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
citire();
for(int i=1; i<=n; i++)
{
if(!ok[i])
{
rez++;
dfs(i);
}
}
printf("%d\n",rez);
return 0;
}