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.

<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>(3n1, 000) <tex2html_verbatim_mark>which is the number of nodes of the tree network. The next line contains two integers s <tex2html_verbatim_mark>(1sn)<tex2html_verbatim_mark>and k <tex2html_verbatim_mark>(k1) <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 难度: 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】[Cqoi2017]小Q的棋盘 树上dfs+贪心

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

  4. Uva 网络(Network,Seoul 2007,LA 3902)

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

  5. UVaLive 3902 Network (无根树转有根树,贪心)

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

  6. poj3417 Network 树上差分+LCA

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

  7. CF E .Tree with Small Distances(树上的贪心)

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

  8. BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]

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

  9. [UVALive 3902] Network

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

随机推荐

  1. windows下virtualenv使用报错

    virtualenv为python提供了一个独立的虚拟环境,使各种python依赖库的安装相互独立.在家里ubuntu上安装一切正常,但在公司的win7上安装总是报以下错误: "D:\Pro ...

  2. Could not load db driver class: com.mysql.jdbc.Driver解决方法

    14/03/26 22:43:24 ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeException: Could ...

  3. HW2.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { final dou ...

  4. POJ2250 - Compromise(LCS+打印路径)

    题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...

  5. Java 动态生成 复杂 .doc文件

    阅读目录 1.word 里面调整好排版,包括你想生成的动态部分,还有一些不用生成的规则性的文字 2. 将 word 文档保存为 xml 3.用 Firstobject free XML edito 打 ...

  6. insert into ... on duplicate key update 与 replace 区别

    on duplicate key update:针对主健与唯一健,当插入值中的主健值与表中的主健值,若相同的主健值,就更新on duplicate key update 后面的指定的字段值,若没有相同 ...

  7. IOPS=(Queue Depth)/(IO latency)

    IO 延迟:存储设备的IO延迟 Queue Depth:磁盘控制器所发出的批量指令的最大条数 IOPS:磁盘设备每秒的IO 三者之间的关系:IOPS=(Queue Depth)/(IO latency ...

  8. 标准I/O之实现细节

    在UNIX系统中,标准I/O库最终都要调用文件I/O(read.write等).每个标准I/O流都有一个与其相关联的文件描述符,可以对一个流调用fileno函数以获得其描述符. 注意,fileno不是 ...

  9. iOS自定义UICollectionViewLayout之瀑布流

    目标效果 因为系统给我们提供的 UICollectionViewFlowLayout 布局类不能实现瀑布流的效果,如果我们想实现 瀑布流 的效果,需要自定义一个 UICollectionViewLay ...

  10. Hadoop卸载或增加节点

    卸载节点或者增加节点: 方式一:静态的增添删除:将集群关闭,修改配置文件(etc/hadoop/slaves),重新启动集群(很黄很暴力,不够人性化). 方式二:动态的增加和卸载节点. 卸载DataN ...