• 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.

    epsfbox{p3902.eps}<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>(3$ le$n$ le$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$ le$s$ le$n)<tex2html_verbatim_mark>and k <tex2html_verbatim_mark>(k$ ge$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表避开“按深度排序”的操作。

    代码如下:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 1000 + 10;
     8 vector<int> gr[maxn], nodes[maxn];
     9 int n, s, k, fa[maxn];
    10 bool covered[maxn];
    11 
    12 // 无根树转有根树,计算fa数组,根据深度把叶子结点插入nodes表里
    13 void dfs(int u, int f, int d) {
    14   fa[u] = f;
    15   int nc = gr[u].size();
    16   if(nc == 1 && d > k) nodes[d].push_back(u);
    17   for(int i = 0; i < nc; i++) {
    18     int v = gr[u][i];
    19     if(v != f) dfs(v, u, d+1);
    20   }
    21 }
    22 
    23 void dfs2(int u, int f, int d) {
    24   covered[u] = true;
    25   int nc = gr[u].size();
    26   for(int i = 0; i < nc; i++) {
    27     int v = gr[u][i];
    28     if(v != f && d < k) dfs2(v, u, d+1); // 只覆盖到新服务器距离不超过k的结点
    29   }
    30 }
    31 
    32 int solve() {
    33   int ans = 0;
    34   memset(covered, 0, sizeof(covered));
    35   for(int d = n-1; d > k; d--) 
    36     for(int i = 0; i < nodes[d].size(); i++) {
    37       int u = nodes[d][i];
    38       if(covered[u]) continue; // 不考虑已覆盖的结点
    39 
    40       int v = u;
    41       for(int j = 0; j < k; j++) v = fa[v]; // v是u的k级祖先
    42       dfs2(v, -1, 0); // 在结点v放服务器
    43       ans++;
    44     }
    45   return ans;
    46 }
    47 
    48 int main() {
    49   int T;
    50   scanf("%d", &T);
    51   while(T--) {
    52     scanf("%d%d%d", &n, &s, &k);
    53     for(int i = 1; i <= n; i++) { gr[i].clear(); nodes[i].clear(); }
    54     for(int i = 0; i < n-1; i++) {
    55       int a, b;
    56       scanf("%d%d", &a, &b);
    57       gr[a].push_back(b);
    58       gr[b].push_back(a);
    59     }
    60     dfs(s, -1, 0);
    61     printf("%d
    ", solve());
    62   }
    63   return 0;
    64 }
     
  • 相关阅读:
    MVC应用程序使用Entity Framework
    @Styles的nameSpace是什么
    创建第一个MVC应用程序
    计算DataTable某列的值(SUM)
    DropDownList 控件的SelectedIndexChanged事件触发不了
    在类中使用Response.Redirect()方法
    控制某个panel的display样式
    获取指定日期下个月份的第一天
    字符串如何还原为中文
    判断字符串中包含3个连续(升、降)或相同的数字
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3202720.html
Copyright © 2020-2023  润新知