Pagini recente » Cod sursa (job #569895) | Cod sursa (job #2971184) | Cod sursa (job #2892124) | Cod sursa (job #1916621) | Cod sursa (job #2602460)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,cnt,viz[100005];
typedef struct nod{
int x;
nod *a;
} *pNod;
pNod v[100005];
void add(pNod &dest, int val)
{
pNod p;
p = new nod;
p -> x = val;
p -> a = dest;
dest = p;
}
void citire()
{
fin >> n >> m;
int x, y;
for (int i=1;i<=m;i++){
fin >> x >> y;
add(v[x], y);
add(v[y], x);
}
}
void dfs(int nod)
{
pNod p;
viz[nod] = 1;
for (p = v[nod];p != NULL;p = p->a)
if (!viz[p->x])
dfs(p->x);
}
int main()
{
citire();
for (int i=1;i<=n;i++){
if (!viz[i]){
cnt++;
dfs(i);
}
}
fout << cnt << '\n';
return 0;
}