Pagini recente » Cod sursa (job #2924223) | Cod sursa (job #3260830) | Cod sursa (job #552824) | Istoria paginii summer-challenge-2009/clasament/runda-2 | Cod sursa (job #146451)
Cod sursa(job #146451)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const long MAX = 100010;
long U[MAX];
vector<long> G[MAX];
long n,m,nr;
void dfs(long x) {
if ( U[x] ) return;
U[x] = true;
for_each(G[x].begin(), G[x].end(), dfs);
}
int main() {
freopen("dfs.in", "r", stdin);
scanf("%ld %ld", &n, &m);
while ( m-- ) {
long x,y;
scanf("%ld %ld", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
fclose(stdin);
for (long i=1; i<=n; ++i)
if ( !U[i] )
nr++, dfs(i);
fprintf(fopen("dfs.out","w"), "%ld\n", nr);
return 0;
}