This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC Optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pairll;
typedef pair<ll,pairll>pairlll;
typedef pair<pairll,pairll>pairllll;
typedef long double ld;
typedef pair<ll,string>pairls;
#define INF 1000000000000007
#define MOD 1000000007
#define pb push_back
#define fr first
#define sc second
#define endl '\n'
ll n,m,t[1507][1507],res;
vector<ll>v[1507];
void S(ll x,ll y){
for(int i=0;i<v[x].size();i++){
if(v[x][i]!=y)S(v[x][i],x);
}
t[x][0]=1;
for(int i=0;i<v[x].size();i++){
if(v[x][i]!=y)t[x][0]+=t[v[x][i]][m-1];
}
for(int i=1;i<m;i++){
if(i*2<m){
t[x][i]=0;
ll h=0;
for(int j=0;j<v[x].size();j++){
if(v[x][j]!=y)h+=t[v[x][j]][m-i-1];
}
for(int j=0;j<v[x].size();j++){
if(v[x][j]!=y){
t[x][i]=max(t[x][i],h-t[v[x][j]][m-i-1]+t[v[x][j]][i-1]);
}
}
}
}
return ;
}
int main() {
cin>>n>>m;
for(int i=1;i<n;i++){
ll l;
cin>>l;
v[l].pb(i);
v[i].pb(l);
}
S(0,0);
res=0;
for(int i=0;i<m;i++){
res=max(res,t[0][i]);
}
cout<<res<<endl;
}
Compilation message (stderr)
catinatree.cpp: In function 'void S(ll, ll)':
catinatree.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
catinatree.cpp:31:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
catinatree.cpp:38:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j=0;j<v[x].size();j++){
| ~^~~~~~~~~~~~
catinatree.cpp:41:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int j=0;j<v[x].size();j++){
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |