补上了这一道原题,感觉弱化版的要简单好多。

神贪心:

我们设$cov_{x, i}$表示在$x$的子树中与$x$距离为$i$的还没有被覆盖到的结点个数,设$rem_{x, i}$表示在$x$的子树中与$x$的距离为$i$的已经设立的消防站还能覆盖的点数。

对于每一个$x$,我们考虑让它去管辖与它距离为$k$的点(即$cov_{x, k}$),因为这些点是必须由$x$来管辖的(即使在$x$的父亲处设立的消防站也管辖不到),容易知道需要这样的消防站的个数为$\left \lceil \frac{cov_{x, k}}{s} \right \rceil$。

那么设立了这样的消防站之后$x$的$rem$肯定会有剩余,我们直接贪心地去用$rem$覆盖$cov$,如果想要使效果最大化,那么用$rem_{x, i}$来抵消$cov_{x, i/i - 1}$就好了,其他的让$x$的父亲来抵消,可以知道这样子答案一定不会变差。

最后看看$1$号根节点有多少还不能覆盖的(设为$cnt$),答案加上$\left \lceil \frac{cnt}{s} \right \rceil$就好。

注意$rem, cov, ans$乘起来会爆$int$。

时间复杂度$O(nk)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 1e5 + ;
const int M = ; int n, s, k, tot = , head[N];
ll ans = 0LL, rem[N][M], cov[N][M]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x, int fat) {
cov[x][] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x); for(int j = ; j <= k; j++)
cov[x][j] += cov[y][j - ];
for(int j = k; j >= ; j--)
rem[x][j - ] += rem[y][j];
} ll now = (cov[x][k] + s - ) / s;
ans += now, rem[x][k] += now * s; for(int i = ; i <= k; i++) {
if(!rem[x][i]) continue;
for(int j = i; j >= && (j >= i - || x == ); j--) {
if(cov[x][j] >= rem[x][i]) {
cov[x][j] -= rem[x][i];
rem[x][i] = ;
break;
}
rem[x][i] -= cov[x][j];
cov[x][j] = ;
}
}
} int main() {
read(n), read(s), read(k);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
} dfs(, ); ll now = ;
for(int i = ; i <= k; i++)
now += cov[][i];
ans += (now + s - ) / s; printf("%lld\n", ans);
return ;
}

Luogu 3479 [POI2009]GAS-Fire Extinguishers的更多相关文章

  1. Luogu P3489 [POI2009]WIE-Hexer 最短路

    https://www.luogu.org/problemnew/show/P3489 普通的最短路,不过我觉得这个复杂度按道理来说边数不应该是m*2^13吗,不知道是数据比较水还是实际上能证明复杂度 ...

  2. 解题:POI 2009 Fire Extinguishers

    题面 洛谷数据非常水,建议去bzoj 我第一眼一看这不是那个POI2011的升级版吗(明明这个是2009年的,应该说那个是这个的弱化版,果然思想差不多. 因为$k$很小,可以考虑每个间隔距离来转移.我 ...

  3. luogu 3488 [POI2009]LYZ-Ice Skates 线段树 + 思维

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin), ...

  4. Luogu 3942 将军令

    之前写那个(Luogu 2279) [HNOI2003]消防局的设立的时候暴力推了一个树形dp,然后就导致这个题不太会写. 贪心,先把树建出来,然后考虑按照结点深度排个序,每次取出还没有被覆盖掉的深度 ...

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

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

  6. [POI2009]救火站Gas

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

  7. [luogu]P1053 篝火晚会[数学][群论]

    [luogu]P1053 篝火晚会 题目描述 佳佳刚进高中,在军训的时候,由于佳佳吃苦耐劳,很快得到了教官的赏识,成为了“小教官”.在军训结束的那天晚上,佳佳被命令组织同学们进行篝火晚会.一共有n个同 ...

  8. [luogu]P1016 旅行家的预算[贪心]

    [luogu]P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能 ...

  9. [LeetCode] Gas Station 加油站问题

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

随机推荐

  1. UVALive 3635 Pie(二分法)

    简单的二分法应用,循环1000次精度就满足要求了. #include<iostream> #include<cstdio> #include<cstdlib> #i ...

  2. UVA 11176 Winning Streak

    #include <iostream> #include <stdio.h> #include <cstring> #define N 501 using name ...

  3. BZOJ1280: Emmy卖猪pigs

    BZOJ1280: Emmy卖猪pigs https://lydsy.com/JudgeOnline/problem.php?id=1280 分析: 这题感觉还好,因为是有时间顺序,所以拆点做最大流即 ...

  4. LeetCode Base 7

    原题链接在这里:https://leetcode.com/problems/base-7/#/description 题目: Given an integer, return its base 7 s ...

  5. Linux终端录屏与播放 script 命令

    本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4107700.html ,转载请保留此声明. 有时候可能想要记录在终端的所有操作包括输出等,将来作为视频播 ...

  6. 手机访问PC网站自动跳转到手机网站代码(转)

    4G时代,手机网站已经非常普遍了,一般手机网站都有一个二级域名来访问,比如 m.16css.com 如果手机直接访问www.16css.com 就是PC网站,在手机上浏览电脑版网站体验非常不好. 如果 ...

  7. bzoj 4827 [Hnoi2017]礼物——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4827 式子就是 \sum_{i=0}^{n-1}(a[ i ] - b[ i+k ] + c ...

  8. c# Chart 服务器端动态创建ChartArea

    1 aspx <x:ContentPanel ShowBorder="true" ShowHeader="false" ID="ContentP ...

  9. java代码I/O流类

    package com.aini; //流类rr //流操作的步骤: /*1.找到指定File 2.实例化字节流.InputStream/OutputStream/Reader/Writer 3.读/ ...

  10. HTTP-Runoob:教程

    ylbtech-HTTP-Runoob:教程 1.返回顶部 1. HTTP 教程 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络 ...