D_L3

SDA - test 6 - 2020-2021 - Премахване на ребра

Jan 8th, 2024
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 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. vector<bool> visited(100, false);
  12. int children[100]{ 0 };
  13.  
  14. int countChildren(int a) {
  15. visited[a] = true;
  16. if (graph[a].empty()) {
  17. return 0;
  18. }
  19.  
  20. for (int child : graph[a]) {
  21. if(!visited[child])
  22. children[a] += countChildren(child) + 1;
  23. }
  24. return children[a];
  25. }
  26.  
  27. int main() {
  28. int v, e, a, b;
  29. cin >> v >> 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. countChildren(1);
  36. int count = 0;
  37. for (int i = 0; i < v; i++) {
  38. if (children[i] % 2 == 1)
  39. count++;
  40. }
  41. cout << count - 1;
  42. return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment