Pagini recente » Cod sursa (job #184646) | Cod sursa (job #1025214) | Cod sursa (job #2747826) | Cod sursa (job #896078) | Cod sursa (job #3214860)
#include <bits/stdc++.h>
using namespace std;
const int max_size = 1e5 + 20;
int viz[max_size];
vector <int> mc[max_size];
void dfs (int nod)
{
viz[nod] = 1;
for (auto f : mc[nod])
{
if (viz[f] == 1)
{
continue;
}
dfs(f);
}
}
void solve ()
{
int n, m;
cin >> n >> m;
int ans = 0;
while (m--)
{
int x, y;
cin >> x >> y;
mc[x].push_back(y);
mc[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
if (viz[i] == 0)
{
ans++;
dfs(i);
}
}
cout << ans;
cout << '\n';
}
signed main ()
{
#ifdef LOCAL
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#else
freopen("dfs.in", "r", stdin);
freopen("dfs.out", "w", stdout);
#endif // LOCAL
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long tt;
//cin >> tt;
tt = 1;
while (tt--)
{
solve();
}
return 0;
}