之前写那个(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. L107

    It is advisable to take an open- minded approach to new idea. 对新思想采取不存先入之见的态度是明智的.That said, the com ...

  2. unity3d IO操作

             前几天有个朋友问我为什么在IOS平台中可以正常的读写文件可是在Android平台中就无法正常的读写.当时因为在上班所以我没时间来帮他解决,晚上回家后我就拿起安卓手机真机调试很快就定位 ...

  3. 团队队列(列和map结合的经典运用)

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  4. BZOJ2342:[SHOI2011]双倍回文

    浅谈\(Manacher\):https://www.cnblogs.com/AKMer/p/10431603.html 题目传送门:https://www.lydsy.com/JudgeOnline ...

  5. JDBC小常识

    1 JDBC连接数据库6步 Load the JDBC Driver Establish the Database Connection Create a Statement Object Execu ...

  6. 设置eclipse中jsp/html文件好看的自动排版

    注:本文转载于ieayoio,原文链接:https://blog.csdn.net/ieayoio/article/details/49930587#8912689 eclipse中jsp文件代码的排 ...

  7. 获得Oracke中刚插入的ID ---> GetInsertedID()

    (1)首先 需要创建序列: CREATE SEQUENCE SE_TD_POWER MINVALUE 1 NOMAXVALUE START WITH 1 INCREMENT BY 1 NOCYCLE ...

  8. Mybatis代码学习

    Mybatis架构学习 MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架.MyBatis 封装了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.可以对配置和原生Map使用 ...

  9. HBase之八--(1):HBase二级索引的设计(案例讲解)

    摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...

  10. Intellij IDEA 发布后的项目在哪里

    Intellij IDEA 中使用 tomcat 并发布项目后,项目并没有出现在在 webapps 文件夹中,如果没有手动修改过部署目录的话,idea的真实部署目录为 File---->Proj ...