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>
#define maxn 100005
using namespace std;
vector<vector<int> > prv, sum_f, suf, f;
int n, D;
int tmp[maxn];
vector<int> adj[maxn];
void dfs(int u, int dad) {
for(int v:adj[u]) if(v != dad) dfs(v, u);
f[u][0] = 1;
int cnt = 0;
for(int v:adj[u]) if(v != dad) {
f[u][0] += sum_f[v][D - 1];
tmp[++cnt] = v;
}
for(int i=1;i<=cnt+1;i++)
for(int j=1;j<=D;j++) prv[i][j] = 0, suf[i][j] = 0;
for(int i=1;i<=cnt;i++) {
for(int j=0;j<=D;j++) prv[i][j] = prv[i-1][j] + sum_f[tmp[i]][j];
}
for(int i=cnt;i>=1;i--) {
for(int j=1;j<=D;j++) suf[i][j] = suf[i+1][j] + sum_f[tmp[i]][j];
}
for(int i=1;i<=D;i++) {
for(int j=1;j<=cnt;j++) {
f[u][i] = max(f[u][i], f[tmp[j]][i - 1] + prv[j-1][max(max(i, D - i) - 1, 0)] + suf[j + 1][max(max(i, D - i) - 1, 0)]);
}
}
// if(u == 1) cout<<cnt;
sum_f[u][D] = f[u][D];
for(int i=D-1;i>=0;i--) sum_f[u][i] = max(sum_f[u][i + 1], f[u][i]);
}
void sub2() {
dfs(1, 0);
int ds = 0;
for(int j=0;j<=D;j++) ds = max(ds, f[1][j]);
cout<<ds;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>D;
D = min(D, 2 * n);
prv.resize(n + 5, vector<int> (D + 5, 0));
sum_f.resize(n + 5, vector<int> (D + 5, 0));
f.resize(n + 5, vector<int> (D + 5, 0));
suf.resize(n + 5, vector<int> (D + 5, 0));
for(int i=1;i<n;i++) {
int x;
cin>>x;
++x;
adj[x].push_back(i + 1);
adj[i + 1].push_back(x);
}
sub2();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |