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 FOR(i,a) for (int i=0;i<(a);++i)
#define FORD(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,b,a) for (int i=(b);i>=(a);--i)
const int N = 2*1000*100+5;
vector<int> g[N];
int mark[N];
int depth[N];
void dfs(int v,int p = -1){
if(p!=-1) depth[v] = depth[p]+1;
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if(to == p) continue;
dfs(to,v);
}
}
bool cmp(int a,int b){
return depth[a]>depth[b];
}
bool col[N];
int d;
void go(int v,int dd = 0,int p = -1){
if(dd>=d) return;
mark[v] = 1;
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if(to == p) continue;
go(to,dd+1,v);
}
}
int dp[N][405];
void dfss(int v,int par=-1){
vector<int> gum(d),mx(d),suf(d);
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if (to==par)continue;
dfss(to,v);
FOR(i,d){
gum[i]+=dp[to][i];
if (d-2>=i){
mx[i]=max(mx[i],dp[to][i]-dp[to][d-2-i]);
}
}
}
FOR(i,d){
mx[i]+=gum[d-2-i];
}
FORD(i,d/2){
suf[i]=max(suf[i+1],mx[i]);
}
FOR(i,d){
if (i==0){
dp[v][i]=max(gum[d-1]+1,suf[0]);
}
else if (i>d/2){
dp[v][i]=gum[i-1];
}
else{
dp[v][i]=suf[i-1];
}
}
/*
cout<<v<<endl;
FOR(i,d)cout<<dp[v][i]<<" ";
cout<<endl;*/
}
int main(){
ios_base::sync_with_stdio(false);
int n;
cin>>n>>d;
for(int i = 1;i<=n-1;i++){
int x;
cin>>x;
g[i].push_back(x);
g[x].push_back(i);
}
long long x = n*n/d;
long long y = n*d;
if(d<401){
dfss(0);
cout<<dp[0][0]<<endl;
return 0;
}
dfs(0);
vector<int> v;
for(int i = 0;i<n;i++) v.push_back(i);
sort(v.begin(),v.end(),cmp);
int ans = 0;
int num = 1;
for(int jj = 0;jj<n;jj++){
int i = v[jj];
if(mark[i]) continue;
mark[i] = 1;
ans++;
go(i);
/*queue<pair<int,int> > q;
q.push({i,0});
num++;
col[i] = num;
while(!q.empty()){
pair<int,int> x = q.front();
int gag = x.first;
int dd = x.second;
q.pop();
if(dd>=d) continue;
mark[gag] = 1;
for(int j = 0;j<g[gag].size();j++){
int to = g[gag][j];
if(col[to]!=num){
q.push({to,dd+1});
col[to] = num;
}
}
}*/
// for(int i = 0;i<n;i++) cout<<mark[i]<<" ";
// cout<<endl;
}
cout<<max(1,ans)<<endl;
return 0;
}
Compilation message (stderr)
catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:13:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[v].size();i++){
~^~~~~~~~~~~~
catinatree.cpp: In function 'void go(int, int, int)':
catinatree.cpp:28:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[v].size();i++){
~^~~~~~~~~~~~
catinatree.cpp: In function 'void dfss(int, int)':
catinatree.cpp:39:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[v].size();i++){
~^~~~~~~~~~~~
catinatree.cpp: In function 'int main()':
catinatree.cpp:83:15: warning: unused variable 'x' [-Wunused-variable]
long long x = n*n/d;
^
catinatree.cpp:84:15: warning: unused variable 'y' [-Wunused-variable]
long long y = n*d;
^
catinatree.cpp:95:9: warning: unused variable 'num' [-Wunused-variable]
int num = 1;
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |