Pagini recente » Cod sursa (job #591022) | Cod sursa (job #1105723) | Cod sursa (job #700612) | Cod sursa (job #2343902) | Cod sursa (job #728885)
Cod sursa(job #728885)
#include <vector>
#include <queue>
#include <cstdio>
#define N 100001
using namespace std;
vector<int> graph[N];
bool visited[N];
int n;
void dfs(int node) {
visited[node] = true;
for (vector<int>::iterator it = graph[node].begin();
it != graph[node].end();
it++) {
if (!visited[*it]) {
dfs(*it);
}
}
}
int main() {
freopen("dfs.in", "r", stdin);
freopen("dfs.out", "w", stdout);
int m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
visited[i] = false;
}
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d ", &x, &y);
graph[x - 1].push_back(y - 1);
graph[y - 1].push_back(x - 1);
}
int counter = 0;
while (1) {
bool find = false;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs(i);
counter++;
find = true;
}
}
if (!find)
break;
}
printf("%d\n", counter);
return 0;
}