Pagini recente » Cod sursa (job #1586133) | Cod sursa (job #1351635) | Cod sursa (job #1402341) | Cod sursa (job #2634690) | Cod sursa (job #3156413)
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
const int NMAX = 10e5;
vector<int>G[NMAX];
int vis[NMAX + 1];
ifstream in("dfs.in");
ofstream out("dfs.out");
void DFS(int x)
{
// cout << x << " ";
vis[x] = 1;
for (auto next : G[x])
{
if (!vis[next])
DFS(next);
}
}
int main()
{
int n, m;
in >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
int cc = 0;
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
cc++;
DFS(i);
}
}
out << cc;
return 0;
}