#include <iostream>
#include <vector>
#include <bitset>
#include <map>
using namespace std;
const int N=2e5+10;
int n,d;
vector<int> ma[N],vep[N];
int part[N];
int h[N],dist[N],sz[N];
bitset<N> rem,dead;
int cn;
void dfs(int x,int p=-1)
{
sz[x]=1;
for(auto y:ma[x])
{
if(y!=p)
{
dfs(y,x);
sz[x]+=sz[y];
}
}
}
int find_centriod(int x,int p=-1)
{
for(auto y:ma[x])
{
if(y!=p and !dead[y] and sz[y]>(cn/2))
{
sz[x]-=sz[y];
sz[y]+=sz[x];
return find_centriod(y,x);
}
}
return x;
}
map<int,int> dist1[N];
int tlp;
void compute_distance(int x,int p=-1)
{
for(int y:ma[x])
{
if(p!=y)
{
dist1[tlp][y]=dist1[tlp][x]+1;
compute_distance(y,x);
}
}
}
int solve(int x)
{
cn=sz[x];
int c=find_centriod(x);
dead[c]=1;
dist1[c][c]=0;
tlp=c;
compute_distance(c);
for(int y:ma[x])
if(!dead[y])
part[solve(y)]=c;
return c;
}
void color(int x)
{
int st=x;
while(x>0)
{
dist[x]=min(dist[x],dist1[x][st]);
x=part[x];
}
}
int query(int x)
{
int ans=dist[x];
int st=x;
while(x>0)
{
ans=min(ans,dist1[x][st]+dist[x]);
x=part[x];
}
return ans;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin>>n>>d;
vep[0].push_back(1);
int max_h=0;
for(int i=2;i<=n;i++)
{
int p;
cin>>p;
p++;
h[i]=h[p]+1;
vep[h[i]].push_back(i);
max_h=max(max_h,h[i]);
ma[p].push_back(i);
ma[i].push_back(p);
}
int ans=0;
for(int j=1;j<=n;j++)
dist[j]=1e6;
dfs(1);
solve(1);
for(int cur_h=max_h;cur_h>=0;cur_h--)
{
for(int v:vep[cur_h])
{
if(query(v)>=d)
{
ans++;
color(v);
}
}
}
cout<<ans<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
20312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
20312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
20312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |