Pagini recente » Cod sursa (job #1679302) | Rating Kaplonyi Akos (Shokap) | Cod sursa (job #820003) | Cod sursa (job #2384987) | Cod sursa (job #1704554)
#include <queue>
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, nr;
vector<int> v;
vector<vector<int> > graph;
bool visited[100001] = { false };
void dfs(int node)
{
visited[node] = true;
for (auto& i : graph[node]) {
if (!visited[i])
dfs(i);
}
}
int main()
{
f >> n >> m;
v.resize(n + 1);
graph.resize(n + 1);
for (int i = 1; i <= m; ++i) {
int a, b;
f >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 1; i <= n; ++i) {
if (!visited[i]) {
++nr;
dfs(i);
}
}
g << nr;
return 0;
}