Alex_tz307

1272. Non-Yekaterinburg Subway - Timus

Nov 4th, 2020 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector < vector < int > > G;
  6. vector < bool > viz;
  7.  
  8. void DFS(int node) {
  9.     viz[node] = true;
  10.     for(int next : G[node])
  11.         if(!viz[next])
  12.             DFS(next);
  13. }
  14.  
  15. int main() {
  16.     ios_base::sync_with_stdio(false);
  17.     cin.tie(nullptr);
  18.     cout.tie(nullptr);
  19.     int N, K, M;
  20.     cin >> N >> K >> M;
  21.     G.resize(N + 1);
  22.     viz.resize(N + 1);
  23.     while(K--) {
  24.         int u, v;
  25.         cin >> u >> v;
  26.         G[u].emplace_back(v);
  27.         G[v].emplace_back(u);
  28.     }
  29.     int cnt = 0;
  30.     for(int i = 1; i <= N; ++i)
  31.         if(!viz[i]) {
  32.             ++cnt;
  33.             DFS(i);
  34.         }
  35.     cout << cnt - 1;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment