-
-
Save fredbr/535e0028f0fc1e12a66d071d37d8b55d to your computer and use it in GitHub Desktop.
Natalia e os Animais
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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