[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 ...
随机推荐
- easyUI排序问题
使用easyUI时,需要在点击页面的某一列进行desc或asc排序,那么在jsp中可以把该列js的sortable 设置true. 加在某字段上时,该字段点击时页面会出现一小三角图案 ,此时easyU ...
- nexus3.1私服搭建
原文:http://blog.csdn.net/qqqqq210/article/details/52993337 1.简介 近期公司需要搭建jenkins自动化构建部署,需要搭建nexus私服环境, ...
- idea、jdk、eclispe中空main方法的线程数量不一样,why?
測试代码: public class Test { public static void main(String[] args) { System.out.println(Th ...
- 运行计划中cost计算方法
概念: blevel:二元高度=索引高度-1 clustering_factor:集群因子,通过索引扫面得出的要查询table的blocks数量,clustering_factor接近table的bl ...
- HDU4763 Theme Section 【KMP】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- GCC 编译详解 (转)
GNU CC(简称为Gcc)是GNU项目中符合ANSI C标准的编译系统,能够编译用C.C++和Object C等语言编写的程序.Gcc不仅功能强大,而且可以编译如C.C++.Object C.Jav ...
- oracle 12c 13姨
搞了一下oracle 12c.有些体会还是先记下来. 12c搞搞新意思,弄了个CDB(容器数据库,可不是商务中心CBD哟)和PDB(可插拔数据库).PDB插在CDB里. 简单而言,CDB就是一个数据库 ...
- 理解ValueStack的基本机制
ValueStack基础:OGNL(Object Graphic Navigation Language) OGNL是Struts2中使用的一种表达式语言.它可以用于: · 在JSP页面,使用标签 ...
- HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...
- POJ1417 True Liars —— 并查集 + DP
题目链接:http://poj.org/problem?id=1417 True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submi ...