[POI2009]救火站Gas
Description
给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站。 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上。 3. 每个消防站可以管理至多s个节点。 4. 消防站只能管理距离(两点间最短路径的边数)不超过k的结点。请问至少要设立多少个消防站。
Input
第一行n,s,k。接下来n-1行每行xi,yi描述一条边连接xi与yi。 1<=n<=100000 1<=s<=n 1<=k<=20 1<=xi
Output
一个数,最少的消防站个数。
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
HINT

这题容易想到一个贪心策略,消防站必定先控制距离为k的点,然后距离递减,但是这个东西我们不好维护,所以我们就可以维护两个数组,用这两个数组来互相抵消维护。
转移的时候有一句话需要注意:
for (int i=k;i;i--) more[x][i-1]+=more[son][i];
为什么是i来更新i-1,因为我控制的点有两个方向,一边向儿子,一边向父亲,向父亲方向的点显然要这么更新;由于儿子方向的点被两个数组互相抵消了两个距离的节点,所以这样更新是没有问题的
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e5,M=20;
int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],tot;
ll more[N+10][M+10],need[N+10][M+10];
int n,m,k,Ans;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void dfs(int x,int fa){
need[x][0]=1;
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa) continue;
dfs(son,x);
for (int i=0;i<k;i++) need[x][i+1]+=need[son][i];
for (int i=k;i;i--) more[x][i-1]+=more[son][i];
}
int T=(need[x][k]+m-1)/m;
Ans+=T;
more[x][k]+=1ll*T*m;
for (int i=0;i<=k;i++){
if (more[x][i]){
for (int j=i;~i&&(j>=i-1||x==1);j--){//抵消掉两个距离的节点
if (more[x][i]<=need[x][j]){
need[x][j]-=more[x][i];
more[x][i]=0;
break;
}
more[x][i]-=need[x][j];
need[x][j]=0;
}
}
}
}
int main(){
n=read(),m=read(),k=read();
for (int i=1;i<n;i++){
int x=read(),y=read();
join(x,y),join(y,x);
}
dfs(1,0);
int tot=0;
for (int i=0;i<=k;i++) tot+=need[1][i];
Ans+=(tot+m-1)/m;
printf("%d\n",Ans);
return 0;
}
[POI2009]救火站Gas的更多相关文章
- BZOJ1117 [POI2009]救火站Gas 贪心
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ1117.html 题目传送门 - BZOJ1117 题意 给你一棵树,现在要建立一些消防站,有以下要求: ...
- [BZOJ1117]救火站gas
Description 给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站. 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- NOIP树上问题总结
这几年考了好几次树上问题: NOIP2012 疫情控制(二分答案+倍增+贪心) NOIP2013 货车运输(最大生成树+倍增) NOIP2014 联合权值(勉强算作树形dp的傻逼题) NOIP2015 ...
- 【自编教材】16万8千字的HTML+CSS基础 适合从0到1-可收藏
[图片链接有点小问题,这几天更新,敬请期待!] 目 录 第一章HTML基础 1.1 HTML简介和发展史 1.1.1 什么是HTML 1.1.2 HTML的发展历程 1.1.3 web标准 1.2 开 ...
- [Leetcode] gas station 气站
There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Gas Station
Description: There are N gas stations along a circular route, where the amount of gas at station i i ...
- bzoj 1133: [POI2009]Kon dp
1133: [POI2009]Kon Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 242 Solved: 81[Submit][Status][D ...
随机推荐
- 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...
- Mark 创建路径(c#)-动态分段
http://bbs.esrichina-bj.cn/ESRI/viewthread.php?action=printable&tid=128564 public void CreateRou ...
- payload和formData有什么不同?
最近做项目的时候,在通过post请求向服务端发送数据的时候,请求失败了.错误信息如下: 返回的400(bad request)错误,说明客户端这边发送的请求是有问题的. 和通过jquery中的ajax ...
- react 组件之间传值
谈及React时,就会想到一个很重要的思想,就是组件化思想.它将可以重用的部分进行组件化开发,形成一个个相对独立的组件,那么组件化后,你也会提出些疑问,组件与组件之间,将怎样进行信息的传递呢?下面来介 ...
- 设计模式 之代理(Proxy)模式
为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事.找别人去做,这就是代理.所以,当你写代码的时候.你想保持类的简单性.重用性.你就能够把事件尽量都交给其他类去做.自己仅仅管做好自己的事.也就 ...
- notepad++ 查找引用(Find Reference)(适用于c c++及各类脚本比方lua、python等)
在程序开发过程中,程序猿经经常使用到的一个功能就是查找引用(Find Reference).Visual Studio里面的相应功能是"查找全部引用"(Find All Refer ...
- c语言学习-指针探究
1:指针定义格式:格式:变量类型 *变量名用途:指针变量用于储存地址(only),也就是根据地址值,访问对应的存储空间. 注意.int *p 只能指向int类型的数据: 例: int a = 20; ...
- Hibernate 之 Mapping
转自: http://blog.csdn.net/jnqqls/article/details/8372732 从前面的介绍的Hibernate文章中我们已经对Hibernate有了一个初步的认识, ...
- Python代码分析工具
Python代码分析工具:PyChecker.Pylint - CSDN博客 https://blog.csdn.net/permike/article/details/51026156
- RabbitMQ的远程Web管理与监控工具
RabbitMQ提供了完善的管理和监控工具,分management plugin 和 rabbitmqctl 两种类型的工具. 1.management plugin rabbitmq-manage ...