LA 3902 Network(树上最优化 贪心)
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.

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>(3n
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>(1
s
n)<tex2html_verbatim_mark>and k <tex2html_verbatim_mark>(k
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(树上最优化 贪心)的更多相关文章
- Uva LA 3902 - Network 树形DP 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- LA 3902 Network
人生第一道图论题啊,有木有 题意: 有一个树状网络,有一个原始服务器s,它的服务范围是k 问至少再放多少台服务范围是k的服务器才能使网络中的每个节点都被覆盖掉 解法: 我们以原始服务器为根将其转化成一 ...
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- Uva 网络(Network,Seoul 2007,LA 3902)
#include<iostream> #include<cstring> #include<vector> using namespace std; +; int ...
- UVaLive 3902 Network (无根树转有根树,贪心)
题意:一个树形网络,叶子是客户端,其他的是服务器.现在只有一台服务器提供服务,使得不超k的客户端流畅,但是其他的就不行了, 现在要在其他结点上安装服务器,使得所有的客户端都能流畅,问最少要几台. 析: ...
- poj3417 Network 树上差分+LCA
题目传送门 题目大意:给出一棵树,再给出m条非树边,先割掉一条树边,再割掉一条非树边,问有几种割法,使图变成两部分. 思路:每一条 非树边会和一部分的树边形成一个环,分三种情况: 对于那些没有形成环的 ...
- CF E .Tree with Small Distances(树上的贪心)
题意: 这是一颗有n-1条边的无向树 , 在树上加最少的边使树的1节点到其他节点的距离最多为 2 : 分析:很容易考虑的贪心的做法,但是该如何的贪心呢 ? 我一开始是打算贪心节点的儿子最多那一个 , ...
- BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3522 Solved: 1041[Submi ...
- [UVALive 3902] Network
图片加载可能有点慢,请跳过题面先看题解,谢谢 一道简单的贪心题,而且根节点已经给你了(\(S\)),这就很好做了. 显然,深度小于等于 \(k\) 的都不用管了(\(S\) 深度为0),那么我们只需要 ...
随机推荐
- Codevs 1222 信与信封问题 二分图匹配,匈牙利算法
题目: http://codevs.cn/problem/1222/ 1222 信与信封问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 ...
- Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s 内存限制:1.0GB 总提交次数:227 AC次数:67 平均分:49. ...
- GiftWrapping算法解决二维凸包问题
一.问题描述 凸集(Convex Set): 任意两点的连线都在这个集合内的集合就是一个凸集. ⒈对于一个集合D,D中任意有限个点的线性组合的全体称为D的凸包. ...
- Codeforces Round #226 (Div. 2)A. Bear and Raspberry
/* 贪心的找到相邻两项差的最大值,再减去c,结果若是负数答案为0. */ 1 #include <stdio.h> #define maxn 105 int num[maxn]; int ...
- Import the Add Email and Post Configuration to the SiteMap managed solution -Dynamices CRM
We have prepared a managed solution named Add Email and Post Configuration to SiteMap that you can i ...
- Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...
- Delphi- 连接MySQL数据库BDE
Delphi使用ADO可以连接MSSQL和ACCESS,但似乎不能连接MYSQL和ORACEL,如果要连接MYSQL和ORACLE得使用BDE. 一.连接方法 首先得先安装mysql驱动程序_mysq ...
- IOS GCD 使用(一) 简介
一 GCD简介 GCD(Grand Central Dispatch)是苹果为实现并发编程提供的新技术.从基本功能上讲,GCD有点像NSOperationQueue,他们都允许程序将任务切分为多个单一 ...
- 读取xml格式文件
$v = [xml]get-content d:\vmconfig.xml $v.Domain.Computer.Name =========================== $v.GetElem ...
- 2014上海全国邀请赛题解 HDOJ 5090-5099
HDOJ 5090 水题.从小到大排序,能够填充达到符合条件的.先填充好.填充之后进行调整. 传送门:pid=5090">点击打开链接 #include <cstdio> ...