Pagini recente » Cod sursa (job #2228958) | Cod sursa (job #1672489) | Cod sursa (job #837890) | Cod sursa (job #42380) | Cod sursa (job #2013423)
#include <cstdio>
#include <vector>
using namespace std;
#define MAX 100001
vector <int> lista[MAX];
int n, m, nc;
bool v[MAX];
void dfs(int nod);
int main()
{
freopen("dfs.in", "r", stdin);
freopen("dfs.out", "w", stdout);
int i, a, b;
scanf("%d%d", &n, &m);
for(i=1; i<=m; i++){
scanf("%d%d", &a, &b);
lista[a].push_back(b);
lista[b].push_back(a);
}
for(i=1; i<=n; i++)
if(!v[i]){
nc++;
dfs(i);
}
printf("%d\n", nc);
return 0;
}
void dfs(int nod){
v[nod] = true;
for(int i=0; i<lista[nod].size(); i++)
if(!v[lista[nod][i]])
dfs(lista[nod][i]);
}