Pagini recente » Cod sursa (job #2876361) | Cod sursa (job #1613696) | Cod sursa (job #869016) | Cod sursa (job #864789) | Cod sursa (job #2503877)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
const int nmax = 10000001;
int n, m;
int viz[nmax];
vector <int> h[nmax];
void Citire()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
h[x].push_back(y);
h[y].push_back(x);
}
for (int i = 1; i <= n; i++)
sort(h[i].begin(), h[i].end());
}
void DFS(int k)
{
viz[k] = 1;
for (auto i : h[k])
if (viz[i] == 0) DFS(i);
}
int main()
{
int cnt = 0;
Citire();
for (int i = 1; i <= n; i++)
if(!viz[i])
{
cnt++;
DFS(i);
}
fout << cnt;
return 0;
}