luogu P2279 [HNOI2003]消防局的设立

I’ve known that S.H.E’s just released  \text{I've known that S.H.E's just released } I’ve known that S.H.E’s just released beautiful world this morning in radio.  \text{this morning in radio. } this morning in radio.  But it only has live beta(That’s too bad). \text{But it only has live beta(That's too bad).} But it only has live beta(That’s too bad).

Description \text{Description} Description

Given interger  N  and a tree with  N  points. To give a definition of a watered point, \text{Given interger }N\text{ and a tree with }N\text{ points. To give a definition of a watered point,} Given interger N and a tree with N points. To give a definition of a watered point,
which has a special point to it in distance 2 or shorter. \text{which has a special point to it in distance 2 or shorter.} which has a special point to it in distance 2 or shorter.
Please place special points as less as possible, so that each point of them ALL got watered. \text{Please place special points as less as possible, so that each point of them ALL got watered.} Please place special points as less as possible, so that each point of them ALL got watered.

Solution 2279 \text{Solution 2279} Solution 2279

Consider using Greedy Algorithm. \text{Consider using Greedy Algorithm.} Consider using Greedy Algorithm.
First of all, we push all the points in order by their deeps. \text{First of all, we push all the points in order by their deeps.} First of all, we push all the points in order by their deeps.
For each point, we just pull out the deepest unwatered point, and set its grandfather \text{For each point, we just pull out the deepest unwatered point, and set its grandfather} For each point, we just pull out the deepest unwatered point, and set its grandfather
to a special point. That’s the optimal solution. \text{to a special point. That's the optimal solution.} to a special point. That’s the optimal solution.


Don’t forget to double the memory of the array of edges! \text{Don't forget to double the memory of the array of edges!} Don’t forget to double the memory of the array of edges!

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>

#define reg register

struct node{
	int x,y,next;
}e[2010];
int len=0;
int first[1010];
int h[1010];
int fa[1010];
int n;
int sd;
int ans=0;

void ins(int x,int y){
	e[++len].x=x;e[len].y=y;
	e[len].next=first[x];first[x]=len;
}
void dfs(int f,int x,int hi){
	if(h[x]) return;
	h[x]=hi;fa[x]=f;
	for(reg int i=first[x];i;i=e[i].next)
		dfs(x,e[i].y,hi+1);
}
struct nod{
	int id,h;
	friend bool operator<(const nod a,const nod b){
		return a.h>b.h;
	}
}p[1010];
int ok[1010];
void set(int x){
	for(reg int i=first[x];i;i=e[i].next){
		int y=e[i].y;
		ok[y]=1;
		for(reg int j=first[y];j;j=e[j].next){
			int z=e[j].y;
			ok[z]=1;
		}
	}
	ok[fa[x]]=1;ok[fa[fa[x]]]=1;
	for(reg int i=first[fa[x]];i;i=e[i].next){
		int y=e[i].y;
		ok[y]=1;
	}
}
void work(){
	for(reg int i=1;i<=n;++i)
		p[i]=(nod){i,h[i]};
	std::sort(p+1,p+n+1);
	for(reg int i=1;i<=n;++i){
		if(ok[p[i].id]) continue;
		++ans;
		set(fa[fa[p[i].id]]);
	}
}
int main(){
	memset(first,0,sizeof(first));
	scanf("%d",&n);
	for(reg int i=1;i<n;++i){
		scanf("%d",&sd);
		ins(i+1,sd);ins(sd,i+1);
	}
	memset(h,0,sizeof(h));
	dfs(1,1,1);
	work();
	printf("%d",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值