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 int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, d;
vector <int> adj[200005];
pi mem[200005];
pi dp(int x, int par){
vector <int> tmp;
int cur = 0;
for(auto i : adj[x]){
if(i == par)continue;
pi lmao = dp(i, x);
tmp.push_back(lmao.se);
cur += lmao.fi;
}
int in = 0;
sort(tmp.begin(), tmp.end());
while((int)tmp.size() > in + 1 && tmp[in] + tmp[in + 1] + 2 < d)in++, cur--;
int left = (in < (int)tmp.size() ? tmp[in] + 1 : d);
if(left >= d)left -= d, cur++;
//cout << x << ' ' << cur << ' ' << left << '\n';
return mem[x] = {cur, left};
}
void solve(){
cin >> n >> d;
for(int i = 1; i < n; i++){
int a; cin >> a; a++;
adj[a].push_back(i + 1);
adj[i + 1].push_back(a);
}
cout << dp(1, -1).fi << '\n';
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
catinatree.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
46 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |