D_L3

SDA - 2020 2021 - test5 - task 2

Dec 11th, 2023
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <unordered_set>
  8.  
  9. using namespace std;
  10.  
  11. unordered_map<int, unordered_set<int>> graph;
  12. bool visited[100000] {false};
  13.  
  14. int dfs(int a, int count = 0){
  15.     visited[a] = true;
  16.     count++;
  17.     for(auto i : graph[a]){
  18.         if(!visited[i])
  19.             count = dfs(i, count);
  20.     }
  21.     return count;
  22. }
  23.  
  24. int main() {
  25.     int n, m , a, b;
  26.     cin >> n >> m;
  27.    
  28.     for(int i = 0; i < m; i++){
  29.         cin >> a >> b;
  30.         graph[a].insert(b);
  31.         graph[b].insert(a);
  32.     }
  33.     int res = 0;
  34.     for(int i = 0; i < n; i++){
  35.         if(!visited[i] && dfs(i) > 1)
  36.             res++;
  37.     }
  38.     cout << res;
  39.    
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment