Skip to content

Instantly share code, notes, and snippets.

@fredbr
Created May 2, 2018 22:42
Show Gist options
  • Save fredbr/535e0028f0fc1e12a66d071d37d8b55d to your computer and use it in GitHub Desktop.
Save fredbr/535e0028f0fc1e12a66d071d37d8b55d to your computer and use it in GitHub Desktop.
Natalia e os Animais
#include <bit/stdc++.h>
using namespace std;
const int maxn = 100010;
vector<int> v[maxn];
int vis[maxn];
void dfs(int x)
{
vis[x] = 1;
for (int u : v[x]) if (!vis[u]) dfs(u);
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
int ans = 0;
for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i), ans++;
cout << ans << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment