Luogu 2279 [HNOI2003]消防局的设立 - 贪心
Description
给定一棵树形图, 建若干个消防站, 消防站能够覆盖到距离不超过2的点, 求最少需要建几个消防站才能覆盖所有点
Solution
从深度最深的点开始, 在它的爷爷节点上建, 每建一次都要把能覆盖的点都记录下来。
执行的次数就是答案。
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
using namespace std; const int N = 1e3 + ; int n, f[N], id[N], vis[N], dep[N];
int ans, head[N], tot; struct edge {
int nxt, to;
}e[N]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
} int cmp(int a, int b) {
return dep[a] > dep[b];
} void dfs(int x) {
if(!x) return;
vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int nt = e[i].to;
vis[nt] = ;
}
} void col(int x) {
vis[x] = ; vis[f[x]] = ;
dfs(f[x]); dfs(f[f[x]]);
x = f[f[x]];
vis[x] = ; vis[f[x]] = ;
dfs(f[x]);
vis[f[f[x]]] = ;
for(int i = head[x]; i; i = e[i].nxt) dfs(e[i].to);
} int main()
{
n = rd;
dep[] = ;
for(int i = ; i <= n; ++i) {
f[i] = rd;
dep[i] = dep[f[i]] + ;
add(f[i], i);
}
for(int i = ; i <= n; ++i) id[i] = i;
sort(id + , id + n + , cmp);
for(int i = ; i <= n; ++i) {
if(vis[id[i]]) continue;
ans++;
col(id[i]);
}
printf("%d\n", ans);
}
Luogu 2279 [HNOI2003]消防局的设立 - 贪心的更多相关文章
- [luogu]P2279 [HNOI2003]消防局的设立[贪心]
[luogu]P2279 [HNOI2003]消防局的设立 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两 ...
- luogu 2279 [HNOI2003]消防局的设立 树形dp
就是细节多一些,思路都非常常规. Code: #include <bits/stdc++.h> #define N 1005 #define inf 1061109567 #define ...
- BZOJ 1217: [HNOI2003]消防局的设立( 贪心 )
一个简单的贪心, 我们只要考虑2个消防局设立的距离为5时是最好的, 因为利用最充分. 就dfs一遍, 再对根处理一下就可以了. 这道题应该是SGU某道题的简化版...这道题距离只有2, 树型dp应该也 ...
- [HNOI2003]消防局的设立 (贪心)
[HNOI2003]消防局的设立 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达, ...
- P2279 [HNOI2003]消防局的设立 贪心or树形dp
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- Luogu P2279 [HNOI2003]消防局的设立
这真的是一道SB题.去你的树形DP 我们看到题目就开始考虑贪心,怎么搞? 一个显然的思路,每次找出一个深度最大且未被覆盖的点,然后建一个消防局? 但这样的话,动用简单的人类思维就可以知道:我TM的还不 ...
- 洛谷 2279 [HNOI2003]消防局的设立
Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了 ...
- BZOJ1217或洛谷2279 [HNOI2003]消防局的设立
BZOJ原题链接 洛谷原题链接 该题有两种做法,树形\(DP\)和贪心. 先讲贪心. 先将所有点按深度从大到小排序,然后从大到小依次取出点,若已经被覆盖则跳过,否则就在它的祖父点建立消防站. 考虑如何 ...
- 【luogu P2279 [HNOI2003]消防局的设立】 题解
题目链接:https://www.luogu.org/problemnew/show/P2279 想怎么贪怎么贪 #include <queue> #include <cstdio& ...
随机推荐
- CentsOS6 Tomcat7 报javax.management.InstanceNotFoundException 解决办法
警告: Failed to unregister MBean with name [Catalina:j2eeType=Servlet,name=UploadServlet,WebModule=//l ...
- float double 如何存储
类型float大小为4字节,即32位,内存中的存储方式如下: 符号位(1 bit) 指数(8 bit) 尾数(23 bit) 类型double大小为8字节,即64位,内存布局如下: 符号位(1 ...
- PHP源码安装经常会碰到的问题及解决办法
错误:configure: error: freetype-config not found. 解决:yum install freetype-devel 错误:configure: error: l ...
- 2017面向对象程序设计(Java) 第4周学习指导及要求(2017.9.14-2017.9.18)
学习目标 深入理解程序设计中算法与程序的关系: 深入理解java程序设计中类与对象的关系: 理解OO程序设计的第一个特征:封装: 需要掌握基本使用方法的预定义类有:Math类.String类.Arra ...
- Codeforces Round #499 (Div. 2) C Fly题解
题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...
- orthodb
1.数据库 orthodb数据: odb10v0_levels.tab.gz: NCBI taxonomy nodes where Ortho DB orthologous groups (OGs) ...
- linus jsch上传文件
package com.osplat.util; import java.io.*; import com.jcraft.jsch.*;import com.osplat.bean.Resultmod ...
- c#批量更新list对象sql
注意: 1.语句中"set "后有空格, 2.最后一个if一定有值,且接连的sql字段 无 逗号 3.parameterList.Clear(); /// <summary ...
- 牛客网练习赛43-C(图论)
题目链接:https://ac.nowcoder.com/acm/contest/548/C 题意:有n个知识点,学会每个知识点花T[i],已经学会了其中k个知识点,有m组关系,t1,t2,t3,表示 ...
- 205. Isomorphic Strings (Map)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...