#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 1, L){FOR(KO, 0, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';
#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILE "FILE"
const int N = 2e5 + 5;
const int mod = 1e9 + 9999;
const int oo = 1e18;
struct Modint{
int x;
Modint(){x = 0;}
Modint(int _x){x = (_x + mod) % mod;}
Modint operator + (const Modint &other) const{ return Modint((x + other.x) % mod); }
Modint operator - (const Modint &other) const{ return Modint((x - other.x + mod) % mod); }
Modint operator * (const Modint &other) const{ return Modint((1LL * x * other.x) % mod); }
void operator += (const Modint &other) { *this = *this + other; }
void operator -= (const Modint &other) { *this = *this - other; }
void operator *= (const Modint &other) { *this = *this * other; }
friend ostream& operator << (ostream& os, const Modint &other){
return os << other.x;
}
};
int n, k;
vector<int> g[N];
namespace Subtask12{
static const int M = 1005 + 5;
static int dp[M][M], pre[M][M], h[M];
static int f[M];
void DFS(int u, int par){
FOA(v, g[u]){
DFS(v, u);
h[u] = max(h[v] + 1, h[u]);
}
FOR(i, 0, h[u]){
f[i] = 0;
FOA(v, g[u]){
f[i] += pre[v][max({0LL, i - 1, k - i})];
}
}
dp[u][0] = f[0] + 1;
FOA(v, g[u]){
FOR(i, 1, h[u]){
dp[u][i] = max(dp[u][i], f[i] + dp[v][i - 1] - pre[v][max({0LL, i - 1, k - i})]);
}
}
FOD(i, h[u], 0){
pre[u][i] = max(dp[u][i], pre[u][i + 1]);
}
}
void solve(){
memset(dp, -0x3f, sizeof dp);
memset(pre, -0x3f, sizeof pre);
DFS(1, -1);
int ans = 0;
FOR(i, 0, n){
ans = max(ans, dp[1][i]);
}
cout << ans;
}
}
signed main(){ quickly
if(fopen(FILE".inp", "r")){
freopen(FILE".inp", "r", stdin);
freopen(FILE".out", "w", stdout);
}
cin >> n >> k;
--k;
FOR(i, 2, n){
int x; cin >> x;
g[x + 1].push_back(i);
}
Subtask12::solve();
}
Compilation message (stderr)
catinatree.cpp: In function 'int main()':
catinatree.cpp:96:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(FILE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:97:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(FILE".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |