abckjs
user_6197949313
c_cpp
2 years ago
820 B
4
Indexable
#include <iostream> #include <utility> #include <vector> #include <algorithm> using namespace std; int main () { int n; cin >> n; vector<pair<int, int>> segment; vector<int> count(n, 0); vector<int> level(n, 1); vector<int> leaf; for (int i = 0; i < n - 1; ++ i) { int parents, child; cin >> parents >> child; pair<int, int> p = make_pair(parents, child); segment.push_back(p); count[parents] ++; level[child] = level[parents] + 1; } for (int i = 0; i < count.size(); ++ i) { if (count[i] == 0) { leaf.push_back(level[i]); } } sort(leaf.begin(), leaf.end()); if (leaf[leaf.size() - 1] - leaf[0] <= 1) { cout << "yes"; } else { cout << "no"; } return 0; }
Editor is loading...