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;
#define pii pair<int, int>
#define fr first
#define se second
const int MAXN = 2e5 + 10;
vector<int> adj[MAXN];
int n, d;
pii calc(int node) {
if (adj[node].empty()) {
return {0, 1};
}
vector<pii> dec;
int sum = 0;
for (auto u : adj[node]) {
dec.push_back(calc(u));
}
sort(dec.begin(), dec.end(), greater<pii>());
int lst = 0x3f3f3f3f, ans = 0, mi = 0x3f3f3f3f;
for (auto u : dec) {
if ((lst + u.fr + 2) >= d) {
ans += u.se;
mi = min(lst, u.fr);
lst = u.fr;
} else {
ans += (u.se - 1);
}
}
mi++;
if (mi == d) {
lst = 0;
ans++;
}
return {mi, ans};
}
int32_t main() {
cin >> n >> d;
for (int i = 1 ; i < n; i++) {
int x;
cin >> x;
adj[x].push_back(i);
}
cout << calc(0).se << "\n";
}
Compilation message (stderr)
catinatree.cpp: In function 'std::pair<int, int> calc(int)':
catinatree.cpp:18:9: warning: unused variable 'sum' [-Wunused-variable]
18 | int sum = 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... |