Uva LA 3902 - Network 树形DP 难度: 0
题目
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1903
题意
一棵树,根上有VOD站,要求任意叶节点到VOD站的距离不超过k,问最少建新VOD站数。
思路
1. 令vod[i]为节点i到VOD站的最短距离, 注意,这是在以i为根的树上有VOD站的情况下,如果没有,vod[i]就设为非法。
依据树形DP的思路,如果在该节点(非叶节点)建站,vod[i]=0,否则 vod[i] = min(vod[child] + 1) for child in i.children.(此时注意不要统计没有VOD的子树的非法值)
2. 令urge[i]为以i为根的树中有叶节点还没被满足的情况下,该节点能忍受VOD站在此树外的最远距离。注意如果所有叶节点都被满足了,urge[i]就设为非法。
对于叶节点, urge[i] = k,对于非叶节点,urge[i] = min(urge[child] + 1] for child in i.children),(此时注意不要统计已经被满足的非法值)
3. 如何判断这棵树的孩子们之间有没有相互满足?明显,如果urge[i]和vod[i]都存在,且urge[i] <= vod[i],则说明有兄弟相互满足的情况。
4. 如果孩子们之间无法相互满足?如果urge[i] > 0,说明不紧迫,交给父亲树去办。如果urge[i] = 0,说明需要建新站。
感想
1. 注意边是无向边,因此边数是点数2倍!
代码
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
const int MAXN = 2e3 + ;
const int inf = 0x3ffffff;
int n, k, s;
int vod[MAXN];
int urge[MAXN];
int first[MAXN];
int to[MAXN];
int nxt[MAXN];
int edgeNum; void addEdge(int f, int t) {
nxt[edgeNum] = first[f];
to[edgeNum] = t;
first[f] = edgeNum++;
} int dfs(int root, int fa) {
if (nxt[first[root]] == -) {
urge[root] = k;
vod[root] = inf;
return ;
}
int ans = ;
vod[root] = inf;
urge[root] = inf;
for (int p = first[root]; p != -; p = nxt[p]) {
int t = to[p];
if (t == fa)continue;
ans += dfs(t, root);
if (vod[t] != inf) {
vod[root] = min(vod[t] + , vod[root]);
}
if (urge[t] != inf) {
urge[root] = min(urge[t] - , urge[root]);
}
}
if (urge[root] != inf) {
if (vod[root] != inf && vod[root] <= urge[root]) {
urge[root] = inf;//satisfied
}
else if (urge[root] == && root != s) {
ans ++;
vod[root] = ;//set up a new one;
urge[root] = inf;//satisfied
}
}
return ans;
} int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
int T;
scanf("%d", &T);
for (int ti = ; ti <= T; ti++) {
memset(first, -, sizeof first);
edgeNum = ;
scanf("%d%d%d", &n, &s, &k);
for (int i = ; i < n; i++) {
int f, t;
scanf("%d%d", &f, &t);
addEdge(f, t);
addEdge(t, f);
}
int ans = dfs(s, -);
printf("%d\n", ans); } return ;
}
Uva LA 3902 - Network 树形DP 难度: 0的更多相关文章
- UVa 10859 - Placing Lampposts 树形DP 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 143. Long Live the Queen 树形dp 难度:0
143. Long Live the Queen time limit per test: 0.25 sec. memory limit per test: 4096 KB The Queen of ...
- SGU 149. Computer Network( 树形dp )
题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
- UVa 1292 - Strategic game (树形dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 给定一棵树,选择尽量少的节点,使得每个没有选中的结点至少和一个已选结点相邻. 思路 ...
- poj3417 Network 树形Dp+LCA
题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...
- UVa 12093 Protecting Zonk (树形DP)
题意:给定一个有n个节点的无根树,有两种装置A和B,每种都有无限多个.在某个节点X使用A装置需要C1的花费,并且此时与节点X相连的边都被覆盖.在某个节点X使用B装置需要C2的花费,并且此时与节点X相连 ...
- UVA 10859 Placing Lamppost 树形DP+二目标最优解的求解方案
题意:给定一个无向,无环,无多重边,要求找出最少的若干点,使得,每条边之中至少有一个点上有街灯.在满足上述条件的时候将还需要满足让两个点被选择的边的数量尽量多. 题解: 对于如何求解最小的节点数目这点 ...
- UVA 10859 - Placing Lampposts 树形DP、取双优值
Placing Lampposts As a part of the mission ‘Beautification of Dhaka City’, ...
随机推荐
- 力扣(LeetCode) 849. 到最近的人的最大距离
在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的. 至少有一个空座位,且至少有一人坐在座位上. 亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上. 返回 ...
- npm升级package.json依赖包到最新版本号
转载自:https://blog.csdn.net/syaivin/article/details/79388244?utm_source=blogxgwz1 1.安装: npm install -g ...
- CSS/让一个盒子消失的5中方法
1.display:none; 2.visibility:hidden; //这种方法隐藏了还是会占位的 3.raba(0,0,0,0.5); //可以调节a来改变透明度 a的取值 ...
- 如何调节tomcat初始内存
1.linux下调节tomcat初始内存大小 linux下tomcat的运行文件为catalina.sh,打开文件,在文件靠近顶部找到“ JAVA_OPTS ”字样,在它后面添加如下内容即可 # JA ...
- [mybatis-spring] Transaction 事务/事务处理/事务管理器
使用mybatis-spring的主要原因之一就是: mybatis-spring允许mybatis参与到spring 事务中. mybatis-spring leverage[use (someth ...
- Matlab-4:追赶法(crout分解)工具箱
function x=chase (a,b,c,f) % the method of chaase******************************* % a, b, c,分别是是方程组的下 ...
- 使用org.apache.poi导出Excel表格
public HSSFWorkbook MakeExcel(List<TransactionLogVO> logList) { // SimpleDateFormat sdf = new ...
- Leetcode 127 **
class Solution { public: int ladderLength(string beginWord, string endWord, vector<string>& ...
- 鼠标放上改变Button的大小
private void enterBtn_MouseLeave(object sender, MouseEventArgs e) { enterBtn.Wi ...
- 压力测试之ab命令
网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种设置不当所暴露出的问题. ab是apache自带的压力测试工具.ab非常实用,它不 ...