Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <queue>
- using namespace std;
- struct Node {
- int height;
- vector<int> edges;
- //int left = 0, right = 0;
- bool leaf = false;
- };
- vector<Node> g;
- void setHeight(int p, int h) {
- g[p].height = h;
- if (g[p].edges.empty()) g[p].leaf = true;
- //if(g[p].left == 0 && g[p].right == 0) g[p].leaf = true;
- //if(g[p].left != 0) setHeight(g[p].left, h+1);
- //if(g[p].right!=0) setHeight(g[p].left, h+1);
- for (int v : g[p].edges) setHeight(v, h + 1);
- }
- int main() {
- int n;
- cin >> n;
- int cnt = 0;
- g.resize(n+1);
- queue<int> parent_que;
- parent_que.push(0);
- while (cnt < n - 1) {
- int a, b;
- cin >> a >> b;
- //int l1, r1;
- //cin >> l >> r;
- //if(l > r){
- // int t = l;
- // l = r;
- // r = t;
- //}
- if (a != 0) {
- g[parent_que.front()].edges.push_back(a);
- parent_que.push(a);
- cnt++;
- }
- if (b != 0) {
- g[parent_que.front()].edges.push_back(b);
- parent_que.push(b);
- cnt++;
- }
- parent_que.pop();
- }
- setHeight(0, 1);
- int minH = 17, maxH = 0;
- for (auto k : g) {
- if (k.leaf) {
- minH = min(minH, k.height);
- maxH = max(maxH, k.height);
- }
- }
- cout << maxH << " " << minH << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement