D_L3

Facebook приятели

Jan 27th, 2024 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 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 <map>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. unordered_map<int, vector<int>> graph;
  13.  
  14. map<int, int> components;
  15.  
  16. void dfs(int start, vector<bool>& visited, int component){
  17.     visited[start] = true;
  18.     components[component]++;
  19.     for(int neigh : graph[start]){
  20.         if(!visited[neigh])
  21.             dfs(neigh, visited, component);
  22.     }
  23. }
  24.  
  25. int main() {
  26.     int n, e, k, a, b;
  27.     cin >> n >> e >> k;
  28.     for(int i = 0; i < e; i++){
  29.         cin >> a >> b;
  30.         graph[a].push_back(b);
  31.         graph[b].push_back(a);
  32.     }
  33.     vector<bool> visited(n, false);
  34.     for(int i = 0; i < n; i++){
  35.         if(!visited[i])
  36.             dfs(i, visited, i);
  37.     }
  38.     int counter = 0;
  39.     for(auto component : components){
  40.         if(component.second % k == 0)
  41.             counter++;
  42.     }
  43.        
  44.     cout << counter;
  45.    
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment