之前写那个(Luogu 2279) [HNOI2003]消防局的设立的时候暴力推了一个树形dp,然后就导致这个题不太会写。

贪心,先把树建出来,然后考虑按照结点深度排个序,每次取出还没有被覆盖掉的深度最大的结点的第$k$个祖先进行染色,这样子算到的答案一定是最小值。

考虑一个深度很大的结点一定要被覆盖掉,所以可以考虑自下向上进行统计,对应了按照深度排序的条件,而一个点是否要设立军队计算入答案和它深度最大的儿子有关,所有每次贪心地去覆盖就是对的。

要注意覆盖的时候并不是只向下的,向上也应该被覆盖,所以要记录一下判一下一个节点是否在栈中,而不是不能等于父亲。

不会算时间复杂度QωQ。

好像是POI一道原题的弱化版(Luogu 3479 [POI2009]GAS-Fire Extinguishers)

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 1e5 + ; int n, k, tot = , head[N], ans = , fa[N], dep[N], a[N];
bool vis[N], ins[N]; 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;
} bool cmp(const int x, const int y) {
return dep[x] > dep[y];
} void dfs(int x, int fat, int depth) {
fa[x] = fat, dep[x] = depth;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x, depth + );
}
} void cov(int x, int dis) {
if(dis < ) return;
vis[x] = ins[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
// if(y == fa[x]) continue;
if(!ins[y]) cov(y, dis - );
}
ins[x] = ;
} inline void solve() {
for(int i = ; i <= n; i++) a[i] = i;
sort(a + , a + + n, cmp); /* for(int i = 1; i <= n; i++)
printf("%d ", a[i]);
printf("\n"); */ for(int i = ; i <= n; i++) {
int x = a[i];
if(vis[x]) continue;
for(int j = k; j >= ; j--, x = fa[x]);
cov(x, k);
ans++;
}
} int main() {
read(n), read(k);
int t; read(t);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
} dfs(, , ); /* for(int i = 1; i <= n; i++)
printf("%d ", dep[i]);
printf("\n"); */ solve(); printf("%d\n", ans);
return ;
}

Luogu 3942 将军令的更多相关文章

  1. Luogu P3942 将军令

    题目 维护每个点子树中最深的没有被覆盖的点(仅计算这条链上的关键点)的距离. 若\(u\)为关键点,则\(d_u=-k-1\). 记录\(mx=\max\limits_{v\in son_u}d_v+ ...

  2. 【题解】将军令 Luogu P3942 (未完成)

    历史/落在/赢家/之手 至少/我们/拥有/传说 谁说/败者/无法/不朽 拳头/只能/让人/低头 念头/却能/让人/抬头 抬头/去看/去爱/去追 你心中的梦 将军令 题目描述 又想起了四月. 如果不是省 ...

  3. noip模拟10[入阵曲·将军令·星空](luogu)

    对于这次考试来说,总体考得还是不错的 就是有一个小问题,特判一定要判对,要不然和不判一样,甚至错了还会挂掉30分 还有一个就是时间分配问题,总是在前几个题上浪费太多时间,导致最后一个题完全没有时间思考 ...

  4. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  5. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  6. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  7. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  8. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  9. BZOJ 3942: [Usaco2015 Feb]Censoring

    Description 有两个字符串,每次用一个中取出下一位,放在一个字符串中,如果当前字符串的后缀是另一个字符串就删除. Sol KMP+栈. 用一个栈来维护新加的字符串就可以了.. 一开始我非常的 ...

随机推荐

  1. Leetcode 1014. Capacity To Ship Packages Within D Days

    二分搜索 class Solution(object): def shipWithinDays(self, weights, D): """ :type weights: ...

  2. Object 的一个问题

    var s1 = 'abc';     s1 instanceof String //false var s2 = new String('abc');     s2 instanceof Strin ...

  3. C++继承细节 -1

    为什么基类析构函数最好要使用 virtual 进行修饰? class A { private: ...... public: ~A(); A() {} }; class B : public A { ...

  4. 如何加快Json 序列化?有哪些方法?

    1.使用阿里的fastjson 2.可以通过去除不必要属性加快序列化.如person对象,有id,name,address,我json需要用户姓名,此时序列化的时候就只序列化name,id和addre ...

  5. <Yii 学习>写入日志

    $postStr ='test:'.(empty( $GLOBALS["HTTP_RAW_POST_DATA"])?'':$GLOBALS["HTTP_RAW_POST_ ...

  6. C++语言对C的增强(2)—— const增强、枚举的增强

    1.const基础知识 #include <iostream> int main(void) { //const定义常量--->const意味着只读 const int a; int ...

  7. UV有问题?

    1.检查读取显示贴图的环境与制作贴图环境UV坐标系是否一致. 如:Directx左上角(0,0),右下角(1,1) unity 左下角(0,0),右上角(1,1) 两者互转需要垂直镜像.

  8. 用Python+Django1.9在Eclipse环境下开发web网站

    最近想学习一下python django, 按网上各位大神们的说明,试着做了一下,这里记录下来,做个笔记. 参考 http://www.cnblogs.com/linjiqin/p/3595891.h ...

  9. Oracle存储过程返回

    IF IN_DN_NUMBER IS NOT NULL THEN SELECT COUNT(*) INTO V_HAS FROM SALEFROMSTORE WHERE ORDERID = IN_DN ...

  10. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...