LA 3902 Network(树上最优化 贪心)

时间:2021-09-30 02:02:13

Network

Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes correspond to servers and the terminal nodes correspond to clients. The nodes are numbered from 1 to n <tex2html_verbatim_mark>. Among the servers, there is an original server S <tex2html_verbatim_mark>which provides VOD (Video On Demand) service. To ensure the quality of service for the clients, the distance from each client to the VOD server S <tex2html_verbatim_mark>should not exceed a certain value k <tex2html_verbatim_mark>. The distance from a node u <tex2html_verbatim_mark>to a node v <tex2html_verbatim_mark>in the tree is defined to be the number of edges on the path from u <tex2html_verbatim_mark>to v <tex2html_verbatim_mark>. If there is a nonempty subset C <tex2html_verbatim_mark>of clients such that the distance from each u <tex2html_verbatim_mark>in C <tex2html_verbatim_mark>to S <tex2html_verbatim_mark>is greater than k <tex2html_verbatim_mark>, then replicas of the VOD system have to be placed in some servers so that the distance from each client to the nearest VOD server (the original VOD system or its replica) is k <tex2html_verbatim_mark>or less.

Given a tree network, a server S <tex2html_verbatim_mark>which has VOD system, and a positive integer k <tex2html_verbatim_mark>, find the minimum number of replicas necessary so that each client is within distancek <tex2html_verbatim_mark>from the nearest server which has the original VOD system or its replica.

For example, consider the following tree network.

LA 3902 Network(树上最优化 贪心)<tex2html_verbatim_mark>

In the above tree, the set of clients is {1, 6, 7, 8, 9, 10, 11, 13}, the set of servers is {2, 3, 4, 5, 12, 14}, and the original VOD server is located at node 12.

For k = 2 <tex2html_verbatim_mark>, the quality of service is not guaranteed with one VOD server at node 12 because the clients in {6, 7, 8, 9, 10} are away from VOD server at distance > k <tex2html_verbatim_mark>. Therefore, we need one or more replicas. When one replica is placed at node 4, the distance from each client to the nearest server of {12, 4} is less than or equal to 2. The minimum number of the needed replicas is one for this example.

Input

Your program is to read the input from standard input. The input consists of T <tex2html_verbatim_mark>test cases. The number of test cases (T <tex2html_verbatim_mark>) is given in the first line of the input. The first line of each test case contains an integer n <tex2html_verbatim_mark>(3LA 3902 Network(树上最优化 贪心)nLA 3902 Network(树上最优化 贪心)1, 000) <tex2html_verbatim_mark>which is the number of nodes of the tree network. The next line contains two integers s <tex2html_verbatim_mark>(1LA 3902 Network(树上最优化 贪心)sLA 3902 Network(树上最优化 贪心)n)<tex2html_verbatim_mark>and k <tex2html_verbatim_mark>(kLA 3902 Network(树上最优化 贪心)1) <tex2html_verbatim_mark>where s <tex2html_verbatim_mark>is the VOD server and k <tex2html_verbatim_mark>is the distance value for ensuring the quality of service. In the following n - 1 <tex2html_verbatim_mark>lines, each line contains a pair of nodes which represent an edge of the tree network.

Output

Your program is to write to standard output. Print exactly one line for each test case. The line should contain an integer that is the minimum number of the needed replicas.

Sample Input

2 14
12 2
1 2
2 3
3 4
4 5
5 6
7 5
8 5
4 9
10 3
2 12
12 14
13 14
14 11
14
3 4
1 2
2 3
3 4
4 5
5 6
7 5
8 5
4 9
10 3
2 12
12 14
13 14
14 11

Sample Output

1
0 题目大意:n台机器连成一个树状网络,其中叶节点是客户端,其他结点是服务器。目前有一台服务器正在提供VOD服务,虽然视频本身质量不错,但对于那些离它很远的客户端来说,网络延迟却难以忍受。你的任务是在一些其他服务器上也安装同样的服务,使得每台客户端到最近服务器的距离不超过一个给定的整数k。为了节约成本,安装服务的服务器台数应尽量少。 分析:把无根树变成有根树会有助于解题。本题中已经有了一个天然的根结点:原始VOD服务器。对于那些已经满足条件(即到原始VOD服务器的距离不超过k)的客户端,直接当他们不存在就可以了。
  对于深度最大的结点u,选择u的k级祖先是最划算的(父亲是一级祖先,父亲的父亲是二级祖先)。
  上述算法的一种实现方法:每放一个新服务器,进行一次DFS,覆盖与它距离不超过k的所有结点。注意,本题只需要覆盖叶子,而不需要覆盖中间结点,而且深度不超过k的叶子已经被原始服务器覆盖,所以我们只需要处理深度大于k的叶节点即可。为了让过程更简单,我们可用nodes表避开“按深度排序”的操作。 代码如下:
 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = + ;
vector<int> gr[maxn], nodes[maxn];
int n, s, k, fa[maxn];
bool covered[maxn]; // 无根树转有根树,计算fa数组,根据深度把叶子结点插入nodes表里
void dfs(int u, int f, int d) {
fa[u] = f;
int nc = gr[u].size();
if(nc == && d > k) nodes[d].push_back(u);
for(int i = ; i < nc; i++) {
int v = gr[u][i];
if(v != f) dfs(v, u, d+);
}
} void dfs2(int u, int f, int d) {
covered[u] = true;
int nc = gr[u].size();
for(int i = ; i < nc; i++) {
int v = gr[u][i];
if(v != f && d < k) dfs2(v, u, d+); // 只覆盖到新服务器距离不超过k的结点
}
} int solve() {
int ans = ;
memset(covered, , sizeof(covered));
for(int d = n-; d > k; d--)
for(int i = ; i < nodes[d].size(); i++) {
int u = nodes[d][i];
if(covered[u]) continue; // 不考虑已覆盖的结点 int v = u;
for(int j = ; j < k; j++) v = fa[v]; // v是u的k级祖先
dfs2(v, -, ); // 在结点v放服务器
ans++;
}
return ans;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &s, &k);
for(int i = ; i <= n; i++) { gr[i].clear(); nodes[i].clear(); }
for(int i = ; i < n-; i++) {
int a, b;
scanf("%d%d", &a, &b);
gr[a].push_back(b);
gr[b].push_back(a);
}
dfs(s, -, );
printf("%d\n", solve());
}
return ;
}
 

LA 3902 Network(树上最优化 贪心)的更多相关文章

  1. Uva LA 3902 - Network 树形DP 难度&colon; 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. LA 3902 Network

    人生第一道图论题啊,有木有 题意: 有一个树状网络,有一个原始服务器s,它的服务范围是k 问至少再放多少台服务范围是k的服务器才能使网络中的每个节点都被覆盖掉 解法: 我们以原始服务器为根将其转化成一 ...

  3. 【bzoj4813】&lbrack;Cqoi2017&rsqb;小Q的棋盘 树上dfs&plus;贪心

    题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...

  4. Uva 网络&lpar;Network&comma;Seoul 2007&comma;LA 3902&rpar;

    #include<iostream> #include<cstring> #include<vector> using namespace std; +; int ...

  5. UVaLive 3902 Network &lpar;无根树转有根树,贪心&rpar;

    题意:一个树形网络,叶子是客户端,其他的是服务器.现在只有一台服务器提供服务,使得不超k的客户端流畅,但是其他的就不行了, 现在要在其他结点上安装服务器,使得所有的客户端都能流畅,问最少要几台. 析: ...

  6. poj3417 Network 树上差分&plus;LCA

    题目传送门 题目大意:给出一棵树,再给出m条非树边,先割掉一条树边,再割掉一条非树边,问有几种割法,使图变成两部分. 思路:每一条 非树边会和一部分的树边形成一个环,分三种情况: 对于那些没有形成环的 ...

  7. CF E &period;Tree with Small Distances&lpar;树上的贪心)

    题意: 这是一颗有n-1条边的无向树 , 在树上加最少的边使树的1节点到其他节点的距离最多为 2 : 分析:很容易考虑的贪心的做法,但是该如何的贪心呢 ? 我一开始是打算贪心节点的儿子最多那一个 , ...

  8. BZOJ 1146&colon; &lbrack;CTSC2008&rsqb;网络管理Network &lbrack;树上带修改主席树&rsqb;

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3522  Solved: 1041[Submi ...

  9. &lbrack;UVALive 3902&rsqb; Network

    图片加载可能有点慢,请跳过题面先看题解,谢谢 一道简单的贪心题,而且根节点已经给你了(\(S\)),这就很好做了. 显然,深度小于等于 \(k\) 的都不用管了(\(S\) 深度为0),那么我们只需要 ...

随机推荐

  1. bzoj 3993&colon; &lbrack;SDOI2015&rsqb;星际战争

    #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #def ...

  2. js实现继承的两种方式

    这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...

  3. 『开源』仿SQLServer山寨一个 跨数据库客户端

    002 Laura.SqlForever项目简单介绍 相关文章 <『练手』001 Laura.SqlForever架构基础(Laura.XtraFramework 的变迁)> <『练 ...

  4. 基于jQuery的移动轮播图(支持触屏)

    移动轮播图我看到两款, 一款是无线天猫的m.tmall.com,实现了无缝轮播. 一款是蘑菇街的,没有实现无缝轮播. 我自己重写一个,类似蘑菇街 <!doctype html> <h ...

  5. Linux命令行编辑快捷键

    Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ...

  6. linux下查看文件系统类型

    1. df -hT命令   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G) -T, --pr ...

  7. i2c总线的oled12864屏的u8x8运用总结

    github网址链接 https://github.com/olikraus/u8g2/wiki/u8x8reference#print 用到的库文件 #ifdef U8X8_HAVE_HW_SPI ...

  8. ACM Doing Homework again

    Ignatius刚刚从第30届ACM / ICPC回到学校.现在他有很多作业要做.每个老师给他一个截止作业的截止日期.如果Ignatius在截止日期之后进行了家庭作业,老师将减少他的最终考试成绩.现在 ...

  9. 怎么写自己的CMakeLists&period;txt

    一. 为什么要使用cmake 理论上说,任意一个C++程序都可以用g++来编译.但当程序规模越来越大时,一个工程可能有许多个文件夹和源文件,这时输入的编译命令将越来越长.通常一个小型C++项目可能含有 ...

  10. java mongoTemplate的group统计

    @Service public class MongoCountServiceImpl implements MongoCountService { @Autowired private MongoT ...