Language: C++

#include<bits/stdc++.h>
#define ll long long
#define N 100010
using namespace std;
int n,a[N],k,ans;
int dfn[N],size[N],tot,fk[N],sonk[N],f[N],dep[N];
int s[N],cnt;
int t[N<<2];
struct member
{
	int x;
};
bool operator <(const member &x,const member &y)
{
	return dep[x.x]<dep[y.x];
}
priority_queue<member> qx[N];
struct node
{
	int num,next;
}mp[N];
void init(int x,int y)
{
	mp[++cnt].next=s[x];
	mp[cnt].num=y;
	s[x]=cnt;
}
void dfs(int x,int fa,int d)
{
	f[x]=fa;
	size[x]=1;
	dep[x]=d;
	dfn[x]=++tot;
	if(d==k)fk[x]=sonk[1];
	if(d>k)fk[x]=sonk[fk[fa]];
	int tx=0;
	for(int i=s[x];i;i=mp[i].next)
	{
		tx=1;
		int y=mp[i].num;
		sonk[x]=y;
		dfs(y,x,d+1);
		size[x]+=size[y];
	}
	if(!tx&&d>k)
	{
		member z;
		z.x=x;
		qx.push(z);
	}
}
void change(int x,int lx,int rx,int l,int r)
{
	if(l<=lx&&rx<=r)
	{
		t[x]=1;
		return;
	}
	int mid=(lx+rx)>>1;
	if(l<=mid)change(x<<1,lx,mid,l,r);
	if(mid<r)change(x<<1|1,mid+1,rx,l,r);
}
int ask(int x,int lx,int rx,int p)
{
	if(t[x]||lx==rx)return t[x];
	int mid=(lx+rx)>>1;
	if(p<=mid)return ask(x<<1,lx,mid,p);
	else return ask(x<<1|1,mid+1,rx,p);
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(i>1)init(a[i],i);
	}
	if(a[1]!=1)++ans,a[1]=1;
	dfs(1,1,0);
	while(qx.size())
	{
		member z=qx.top();
		qx.pop();
		int x=z.x;
		if(!ask(1,1,n,dfn[x]))
		{
			x=fk[x];
			++ans;
			change(1,1,n,dfn[x],dfn[x]+size[x]-1);
			x=f[x];
			if(dep[x]>k)
			{
				z.x=x;
				qx.push(z);
			}
		}
	}
	printf("%d",ans);
}

Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:31:9: error: reference to 'size' is ambiguous
   31 |         size[x]=1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:12: note:                 'int size [100010]'
    6 | int dfn[N],size[N],tot,fk[N],sonk[N],f[N],dep[N];
      |            ^~~~
a.cc:43:17: error: reference to 'size' is ambiguous
   43 |                 size[x]+=size[y];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:12: note:                 'int size [100010]'
    6 | int dfn[N],size[N],tot,fk[N],sonk[N],f[N],dep[N];
      |            ^~~~
a.cc:43:26: error: reference to 'size' is ambiguous
   43 |                 size[x]+=size[y];
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:12: note:                 'int size [100010]'
    6 | int dfn[N],size[N],tot,fk[N],sonk[N],f[N],dep[N];
      |            ^~~~
a.cc:49:20: error: request for member 'push' in 'qx', which is of non-class type 'std::priority_queue<member> [100010]'
   49 |                 qx.push(z);
      |                    ^~~~
a.cc: In function 'int main()':
a.cc:80:18: error: request for member 'size' in 'qx', which is of non-class type 'std::priority_queue<member> [100010]'
   80 |         while(qx.size())
      |                  ^~~~
a.cc:82:29: error: request for member 'top' in 'qx', which is of non-class type 'std::priority_queue<member> [100010]'
   82 |                 member z=qx.top();
      |                             ^~~
a.cc:83:20: error: request for member 'pop' in 'qx', which is of non-class type 'std::priority_queue<member> [100010]'
   83 |                 qx.pop();
      |                    ^~~
a.cc:89:52: error: reference to 'size' is ambiguous
   89 |                         change(1,1,n,dfn[x],dfn[x]+size[x]-1);
      |                                                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:12: note:                 'int size [100010]'
    6 | int dfn[N],size[N],tot,fk[N],sonk[N],f[N],dep[N];
      |            ^~~~
a.cc:94:36: error: request for member 'push' in 'qx', which is of non-class type 'std::priority_queue<member> [100010]'
   94 |                                 qx.push(z);
      |                                    ^~~~