Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector < vector < int > > G;
- vector < bool > viz;
- void DFS(int node) {
- viz[node] = true;
- for(int next : G[node])
- if(!viz[next])
- DFS(next);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int N, K, M;
- cin >> N >> K >> M;
- G.resize(N + 1);
- viz.resize(N + 1);
- while(K--) {
- int u, v;
- cin >> u >> v;
- G[u].emplace_back(v);
- G[v].emplace_back(u);
- }
- int cnt = 0;
- for(int i = 1; i <= N; ++i)
- if(!viz[i]) {
- ++cnt;
- DFS(i);
- }
- cout << cnt - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment