제출 #677255

#제출 시각아이디문제언어결과실행 시간메모리
677255CyanmondCat in a tree (BOI17_catinatree)C++17
100 / 100
93 ms19496 KiB
#include <bits/stdc++.h> int main() { int N, D; std::cin >> N >> D; std::vector<int> P(N - 1); std::vector<std::vector<int>> tree(N); for (int i = 0; i < N - 1; ++i) { std::cin >> P[i]; tree[P[i]].push_back(i + 1); } auto dfs = [&](auto &&self, const int v) -> std::pair<int, int> { if (tree[v].size() == 0) { return std::make_pair(1, 1); } std::vector<std::pair<int, int>> children; for (const int t : tree[v]) { children.push_back(self(self, t)); } std::sort(children.begin(), children.end(), std::greater()); const int m = (int)children.size(); int lm = m - 1; for (int i = 1; i < m; ++i) { if (children[i].first + children[i - 1].first < D) { lm = i - 1; break; } } int sum = 0; for (const auto &[a, b] : children) { sum += b; } sum -= m - lm - 1; int nf = children[lm].first; if (nf >= D) { nf = 0; ++sum; } ++nf; return std::make_pair(nf, sum); }; std::cout << dfs(dfs, 0).second << std::endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...