D_L3

SDA - 2022-2023 - test5 - task 2

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