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的点,然后距离递减,但是这个东西我们不好维护,所以我们就可以维护两个数组,用这两个数组来互相抵消维护。

转移的时候有一句话需要注意:

  1. for (int i=k;i;i--) more[x][i-1]+=more[son][i];

为什么是i来更新i-1,因为我控制的点有两个方向,一边向儿子,一边向父亲,向父亲方向的点显然要这么更新;由于儿子方向的点被两个数组互相抵消了两个距离的节点,所以这样更新是没有问题的

  1. /*program from Wolfycz*/
  2. #include<cmath>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<iostream>
  6. #include<algorithm>
  7. #define inf 0x7f7f7f7f
  8. using namespace std;
  9. typedef long long ll;
  10. typedef unsigned int ui;
  11. typedef unsigned long long ull;
  12. inline int read(){
  13. int x=0,f=1;char ch=getchar();
  14. for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
  15. for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
  16. return x*f;
  17. }
  18. inline void print(int x){
  19. if (x>=10) print(x/10);
  20. putchar(x%10+'0');
  21. }
  22. const int N=1e5,M=20;
  23. int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],tot;
  24. ll more[N+10][M+10],need[N+10][M+10];
  25. int n,m,k,Ans;
  26. void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
  27. void dfs(int x,int fa){
  28. need[x][0]=1;
  29. for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
  30. if (son==fa) continue;
  31. dfs(son,x);
  32. for (int i=0;i<k;i++) need[x][i+1]+=need[son][i];
  33. for (int i=k;i;i--) more[x][i-1]+=more[son][i];
  34. }
  35. int T=(need[x][k]+m-1)/m;
  36. Ans+=T;
  37. more[x][k]+=1ll*T*m;
  38. for (int i=0;i<=k;i++){
  39. if (more[x][i]){
  40. for (int j=i;~i&&(j>=i-1||x==1);j--){//抵消掉两个距离的节点
  41. if (more[x][i]<=need[x][j]){
  42. need[x][j]-=more[x][i];
  43. more[x][i]=0;
  44. break;
  45. }
  46. more[x][i]-=need[x][j];
  47. need[x][j]=0;
  48. }
  49. }
  50. }
  51. }
  52. int main(){
  53. n=read(),m=read(),k=read();
  54. for (int i=1;i<n;i++){
  55. int x=read(),y=read();
  56. join(x,y),join(y,x);
  57. }
  58. dfs(1,0);
  59. int tot=0;
  60. for (int i=0;i<=k;i++) tot+=need[1][i];
  61. Ans+=(tot+m-1)/m;
  62. printf("%d\n",Ans);
  63. return 0;
  64. }

[POI2009]救火站Gas的更多相关文章

  1. BZOJ1117 [POI2009]救火站Gas 贪心

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ1117.html 题目传送门 - BZOJ1117 题意 给你一棵树,现在要建立一些消防站,有以下要求: ...

  2. [BZOJ1117]救火站gas

    Description 给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站. 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上 ...

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. NOIP树上问题总结

    这几年考了好几次树上问题: NOIP2012 疫情控制(二分答案+倍增+贪心) NOIP2013 货车运输(最大生成树+倍增) NOIP2014 联合权值(勉强算作树形dp的傻逼题) NOIP2015 ...

  5. 【自编教材】16万8千字的HTML+CSS基础 适合从0到1-可收藏

    [图片链接有点小问题,这几天更新,敬请期待!] 目 录 第一章HTML基础 1.1 HTML简介和发展史 1.1.1 什么是HTML 1.1.2 HTML的发展历程 1.1.3 web标准 1.2 开 ...

  6. [Leetcode] gas station 气站

    There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You ...

  7. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. Gas Station

    Description: There are N gas stations along a circular route, where the amount of gas at station i i ...

  9. bzoj 1133: [POI2009]Kon dp

    1133: [POI2009]Kon Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 242  Solved: 81[Submit][Status][D ...

随机推荐

  1. jmeter的jmx脚本结构解析

    jmeter的jmx脚本是xml文档,简单分析下其结构 xml是树形结构:jmeter界面的树形结构就是xml的结构 一级目录: 二级目录:在一级目录右键后可以看到的,都可以做为二级目录 三级目录.n ...

  2. How can we listen for errors that do not trigger window.onerror?

    原文: http://stackoverflow.com/questions/19141195/how-can-we-listen-for-errors-that-do-not-trigger-win ...

  3. Dell R420 RAID建立以及系统安装

    http://thefallenheaven.blog.51cto.com/450907/1753472 Dell R420的RAID划分,以及系统安装 3块2T的盘,装好硬盘后开机,这里有3种方式去 ...

  4. Java中常见的排序算法

    这是我摘取的一段英文资料.我认为学习算法之前,对各种排序得有个大致的了解: Sorting algorithms are an important part of managing data. At ...

  5. 添加 XML内Rows数据

    public static void addItemToXml(string method,string firstKey,string id,string checkName,string refV ...

  6. my-small.cnf my-medium.cnf my-large.cnf my-huge.cnf

    my-small.cnf my-medium.cnf my-large.cnf my-huge.cnf 是 MySQL 默认的几个配置文件.针对不同配置的服务器可以使用不同的配置文件,将你需要的那一个 ...

  7. python 深浅复制与指针内存

    Python是一门非常好的语言,他的长处在于拥有巨大灵活性的同一时候也拥有无比的严谨性,其它语言规定了非常多语法.告诉你什么情况下,语法就是这种,而Python却用非常少的规定,延伸出非常多语法,有些 ...

  8. 必备java参考资源列表

    现在开始正式介绍这些参考资源. Web 站点和开发人员 Web 门户 网络无疑改变了共享资源和出版的本质(对我也是一样:您正在网络上阅读这篇文章),因此,从每位 Java 开发人员都应该关注的关键 W ...

  9. JDK各版本内容和新特性

    JDK各版本内容和新特性 - yanlzhl - 博客园 https://www.cnblogs.com/yanlzhl/articles/5694470.html    版本JDK1.0:1995年 ...

  10. MongoDB相关的一些技术文章

    安装 win7下安装和配置MongoDB的总结---阿冬专栏