This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<vector<int>> G, children;
pair<int, int> dfs(int u, int D) {
if (children[u].empty()) {
return make_pair(1, 1);
}
pair<int, int> answer = dfs(children[u][0], D);
answer = (answer.second == D ? make_pair(answer.first + 1, 0) : answer);
for (int i = 1; i < (int)children[u].size(); i++) {
auto child = dfs(children[u][i], D);
answer.first += child.first;
if (answer.second + child.second >= D) {
answer.second = min(answer.second, child.second);
} else {
answer.first--;
answer.second = max(answer.second, child.second);
}
}
answer.second++;
return answer;
}
int main() {
cin >> N >> K;
G.resize(N);
children.resize(N);
for (int i = 1; i < N; i++) {
int v; cin >> v;
children[v].push_back(i);
}
cout << dfs(0, K).first << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |