之前写那个(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. 字符集、字符编码、XML中的中文编码

    字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...

  2. CANopenSocket CANopenCommand.c hacking

    /***************************************************************************** * CANopenSocket CANop ...

  3. UVA - 1632 Alibaba (区间dp+常数优化)

    题目链接 设$dp[l][r][p]$为走完区间$[l,r]$,在端点$p$时所需的最短时间($p=0$代表在左端点,$p=1$代表在右端点) 根据题意显然有状态转移方程$\left\{\begin{ ...

  4. BZOJ3302: [Shoi2005]树的双中心

    BZOJ3302: [Shoi2005]树的双中心 https://lydsy.com/JudgeOnline/problem.php?id=3302 分析: 朴素算法 : 枚举边,然后在两个连通块内 ...

  5. [Luogu3674]小清新人渣的本愿

    luogu 题意 给你一个序列a,长度为n,有m次操作,每次询问一个区间是否可以选出两个数它们的差为x,或者询问一个区间是否可以选出两个数它们的和为x,或者询问一个区间是否可以选出两个数它们的乘积为x ...

  6. [Wc2009]shortest

    传送门 终于把这题过了,了了我两年前写堵塞的交通一晚上无果的心结 因为是6要注意蛇皮走位啊!!这种-> S //Achen #include<bits/stdc++.h> #defi ...

  7. java 收集linux信息

    public class MachineCollector  implements Runnable{  private static int DEFAULT_INTERVAL = 30;  priv ...

  8. C# 获取计算机的硬件信息

    /// <summary> /// 获得CPU编号 /// </summary> /// <returns></returns> public stri ...

  9. Azure VM开启资源监控

    目前China的Azure VM资源监控默认是不打开的.本文将介绍如何开启VM的监控功能. 一 Azure VM 打开Azure的Portal页面https://portal.azure.cn,登录后 ...

  10. YUV转换成RGB算法

    YUV转换成RGB void yuvtorgb ( double *rgb,unsigned char *yuv) { int i; rgb[] = ] + + ] - ); // r rgb[] = ...