답안 #924928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924928 2024-02-10T05:03:29 Z Faisal_Saqib Cat in a tree (BOI17_catinatree) C++17
0 / 100
2 ms 11356 KB
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
const int N=2e5+10;
int n,d;
vector<int> ma[N],vep[N];
int part[N],wei[N];
int h[N],dist[N],sz[N];
bitset<N> 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];
		}
	}
}
pair<int,int> find_centriod(int x,int p=-1,int dep=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,dep+1);
		}
	}
	return {x,dep};
}
pair<int,int> solve(int x)
{
	cn=sz[x];
	pair<int,int> cpa=find_centriod(x);
	int c=cpa.first;
	dead[c]=1;
	for(int y:ma[x])
	{
		if(!dead[y])
		{
			auto it=solve(y);
			part[it.first]=c;
			wei[it.first]=it.second;
		}
	}
	return cpa;
}
void color(int x)
{
	int d=0;
	while(x>0)
	{
		dist[x]=min(dist[x],d);	
		d+=wei[x];
		x=part[x];
	}
}
int query(int x)
{
	int ans=dist[x];
	int d=0;
	while(x>0)
	{
		ans=min(ans,dist[x]+d);
		d+=wei[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;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 11356 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 11356 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 11356 KB Output isn't correct
2 Halted 0 ms 0 KB -