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;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#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)
#define trav(i,v) for (auto i : v)
#define all(v) v.begin(),v.end()
#define ad push_back
#define fr first
#define sc second
#define mpr(a,b) make_pair(a,b)
#define pir pair<int,int>
#define all(v) v.begin(),v.end()
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define srng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define y1 EsiHancagorcRepa
const int N=2e5+10;
const ll MOD=1e9+7;
vector<int> g[N];
int dp[N][406];
int n,d;
void dfs(int v,int par=-1){
vector<int> gum(d),mx(d),suf(d);
trav(to,g[v]){
if (to==par)continue;
dfs(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;*/
}
bool col[N];
vector<pir> srt;
void dfs2(int v,int dep=n,int par=-1){
srt.ad({dep,v});
trav(to,g[v]){
if (to==par)continue;
dfs2(to,dep-1,v);
}
}
void dfs3(int v,int dep=0,int par=-1){
if (dep>=d)return;
//cout<<v<<endl;
col[v]=true;
trav(to,g[v]){
if (to==par)continue;
dfs3(to,dep+1,v);
}
}
int main(){
fastio;
srng;
cin>>n>>d;
if (d>=n){
cout<<1<<endl;
return 0;
}
FORT(i,1,n-1){
int p;
cin>>p;
g[p].ad(i);
g[i].ad(p);
}
if (d<=400){
dfs(0);
cout<<dp[0][0]<<endl;
}
else{
dfs2(0);
sort(all(srt));
int ans=0;
FOR(i,srt.size()){
int tv=srt[i].sc;
if (!col[tv]){
ans++;
dfs3(tv);
//cout<<"****"<<endl;
}
}
cout<<ans<<endl;
}
return 0;
}
Compilation message (stderr)
catinatree.cpp: In function 'int main()':
catinatree.cpp:8:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define FOR(i,a) for (int i=0;i<(a);++i)
^
catinatree.cpp:111:9: note: in expansion of macro 'FOR'
FOR(i,srt.size()){
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |