Advertisement
thevals

aisd_zad10.03

Mar 10th, 2023
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. struct Node {
  9.     int height;
  10.     vector<int> edges;
  11.     //int left = 0, right = 0;
  12.     bool leaf = false;
  13. };
  14. vector<Node> g;
  15.  
  16. void setHeight(int p, int h) {
  17.     g[p].height = h;
  18.     if (g[p].edges.empty()) g[p].leaf = true;
  19.     //if(g[p].left == 0 && g[p].right == 0) g[p].leaf = true;
  20.     //if(g[p].left != 0) setHeight(g[p].left, h+1);
  21.     //if(g[p].right!=0) setHeight(g[p].left, h+1);
  22.     for (int v : g[p].edges) setHeight(v, h + 1);
  23. }
  24. int main() {
  25.     int n;
  26.     cin >> n;
  27.     int cnt = 0;
  28.     g.resize(n+1);
  29.     queue<int> parent_que;
  30.     parent_que.push(0);
  31.    
  32.     while (cnt < n - 1) {
  33.         int a, b;
  34.         cin >> a >> b;
  35.         //int l1, r1;
  36.         //cin >> l >> r;
  37.         //if(l > r){
  38.         //  int t = l;
  39.         //  l = r;
  40.         //  r = t;
  41.         //}
  42.         if (a != 0) {
  43.             g[parent_que.front()].edges.push_back(a);
  44.             parent_que.push(a);
  45.             cnt++;
  46.         }
  47.         if (b != 0) {
  48.             g[parent_que.front()].edges.push_back(b);
  49.             parent_que.push(b);
  50.             cnt++;
  51.         }
  52.         parent_que.pop();
  53.        
  54.     }
  55.     setHeight(0, 1);
  56.  
  57.     int minH = 17, maxH = 0;
  58.     for (auto k : g) {
  59.         if (k.leaf) {
  60.             minH = min(minH, k.height);
  61.             maxH = max(maxH, k.height);
  62.         }
  63.     }
  64.     cout << maxH << " " << minH << endl;
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement