[POI2009]救火站Gas 贪心

Description
给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站。 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上。 3. 每个消防站可以管理至多s个节点。 4. 消防站只能管理距离(两点间最短路径的边数)不超过k的结点。请问至少要设立多少个消防站。


Sample Input
12 3 1
1 12
3 8
7 8
8 9
2 12
10 12
9 12
4 8
5 8
8 11
6 8


Sample Output
4


你考虑肯定是一个点他不得不染的时候你才去染他吧,也就是说如果他的K层的节点没被染你肯定先去染他,不然以后就没得染了,并且这样肯定比先前先染了这个节点要优秀。
那你对于每一个点你记录下他染完之后每一层剩多少个节点,每一层需要多少个节点,那你每次继承从子节点完之后,都可以两两之间互相抵消,也就是说假如有一个子节点需要三个节点,另一个子节点多出三个节点,那么就可以互相抵消。
注意这里互相抵消的话,是只能等迫不得已才去抵消,也就是说在层数差小于等于1时采取消掉。
因为你两个节点,如果现在不一定要消,你是完全可以等到他的父节点再消的,但加入你现在消掉,就有可能父亲节点会有更适合的来跟他消掉。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
int _min(int x, int y) {return x < y ? x : y;}
int _max(int x, int y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct edge {
	int x, y, next;
} e[210000]; int len, last[110000];
int K, s, ans;
LL ned[110000][22], ful[110000][22];

void ins(int x, int y) {
	e[++len].x = x, e[len].y = y;
	e[len].next = last[x], last[x] = len;
}

void dfs(int x, int fa) {
	ned[x][0]++;
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(y != fa) {
			dfs(y, x);
			for(int i = 1; i <= K; i++) ful[x][i - 1] += ful[y][i];
			for(int i = 0; i < K; i++) ned[x][i + 1] += ned[y][i];
		}
	}
	if(ned[x][K]) {
		int u = (ned[x][K] + s - 1) / s;
		ful[x][K] += (LL)u * s; ans += u;
	}
	for(int i = 0; i <= K; i++) if(ful[x][i]){
		if(ned[x][i]) {
			if(ful[x][i] >= ned[x][i]) ful[x][i] -= ned[x][i], ned[x][i] = 0;
			else ned[x][i] -= ful[x][i], ful[x][i] = 0;
		}
		if(ful[x][i] && i >= 1 && ned[x][i - 1]) {
			if(ful[x][i] >= ned[x][i - 1]) ful[x][i] -= ned[x][i - 1], ned[x][i - 1] = 0;
			else ned[x][i - 1] -= ful[x][i], ful[x][i] = 0;
		}
	}
}

int main() {
	int n = read();
	s = read(), K = read();
	for(int i = 1; i < n; i++) {
		int x = read(), y = read();
		ins(x, y), ins(y, x);
	} dfs(1, 0);
	for(int i = K; i >= 0; i--) {
		ful[1][i] += ful[1][i + 1];
		if(ned[1][i]) {
			if(ful[1][i] >= ned[1][i]) ful[1][i] -= ned[1][i], ned[1][i] = 0;
			else ned[1][i] -= ful[1][i], ful[1][i] = 0;
		}
	} int uu = 0;
	for(int i = 0; i <= K; i++) uu += ned[1][i];
	ans += (uu + s - 1) / s;
	printf("%d\n", ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值