Pagini recente » Cod sursa (job #963591) | Cod sursa (job #2799097) | Cod sursa (job #1123076) | Cod sursa (job #1654744) | Cod sursa (job #211071)
Cod sursa(job #211071)
#include <stdio.h>
typedef struct nod
{
int n;
nod *next;
}*list;
list v[1000];
int N,M,cnt,viz[1000];
void add(list &x, int y)
{
list p = new nod;
p -> n = y;
p -> next = x;
x = p;
}
void citire()
{
int a,b;
scanf("%d %d",&N,&M);
for(int i = 1; i <= M; i++)
{
scanf("%d %d",&a, &b);
add(v[a],b);
add(v[b],a);
}
}
void dfs(int k)
{
viz[k] = 1;
for(list p = v[k]; p != NULL; p = p -> next)
if(!viz[p -> n])
dfs(p -> n);
}
void solve()
{
for(int i = 1; i <= N; i++)
if(!viz[i])
{
cnt++;
dfs(i);
}
printf("%d\n",cnt);
}
int main()
{
freopen("dfs.in","rt",stdin);
freopen("dfs.out","wt",stdout);
citire();
solve();
return 0;
}