Pagini recente » Cod sursa (job #1484459) | Cod sursa (job #1485228) | Cod sursa (job #1933481) | Cod sursa (job #918349) | Cod sursa (job #2261238)
#include <bits/stdc++.h>
#define NMax 100005
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, cnt;
vector <int> Graph[NMax];
bool viz[NMax];
void DFS(int node)
{
viz[node] = true;
for (int i = 0; i < Graph[node].size(); ++i)
if (!viz[Graph[node][i]])
DFS(Graph[node][i]);
}
//
int main()
{
f >> n >> m;
int x, y;
for (int i = 1; i <= m; ++i) {
f >> x >> y;
Graph[x].push_back(y);
Graph[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
if (!viz[i]) {
++cnt;
DFS(i);
}
g << cnt;
return 0;
}