LA3902 - Network

原创 2015年07月10日 10:17:06

Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal nodes correspond to clients. The nodes are numbered from 1 to n . Among the servers, there is an original server S 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 should not exceed a certain value k . The distance from a node u to a node v in the tree is defined to be the number of edges on the path from u to v . If there is a nonempty subset C of clients such that the distance from each u in C to S is greater than k , 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 or less.

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

For example, consider the following tree network.

\epsfbox{p3902.eps}

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 , 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 . 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 test cases. The number of test cases (T ) is given in the first line of the input. The first line of each test case contains an integer n (3$ \le$n$ \le$1, 000) which is the number of nodes of the tree network. The next line contains two integers s (1$ \le$s$ \le$n) and k (k$ \ge$1) where s is the VOD server and k is the distance value for ensuring the quality of service. In the following n - 1 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

 

#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1010;
int n, s, k;
vector<int>G[MAXN];
int parent[MAXN];
int dept[MAXN];

bool use[MAXN];
void setparent(int xb,int pre,int d)
{
    parent[xb] = pre;
    dept[xb] = d;
    if(d<=k)use[xb] = true;
    for(int i=0; i<G[xb].size(); i++)
    {
        int next = G[xb][i];

        if(next!=pre)
        {
            //有下一个节点就不是叶子节点。
            use[xb] = true;
            setparent(next,xb,d+1);
        }
    }
}
void changeuse(int xb,int pre,int d)
{
    use[xb] = true;
    for(int i=0; i<G[xb].size(); i++)
    {
        int next = G[xb][i];
        if(next!=pre)
        {
           if(d+1<=k)
           changeuse(next,xb,d+1);
        }
    }
}
void insertone(int xb)
{
    int t = k;//cout<<xb<<" "<< dept[xb]<<" -->";
    while(t>0)
    {
        xb = parent[xb];
        t--;
    }//cout<<xb<<endl;
    changeuse(xb,-1,0);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int a, b, i;
        for(i=1; i<=n; i++)G[i].clear();
        scanf("%d%d",&s,&k);
        for(i=1; i<n; i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        memset(use,0,sizeof use);
        memset(parent,0,sizeof parent);
        setparent(s,-1,0);
        /*for(i=1; i<=n; i++)
        {
            if(use[i])
            {
                cout<<i<<"-->true"<<endl;
            }else{
                cout<<i<<"-->false"<<endl;
            }
        }*/
        int ans = 0;
        while(true)
        {
            int maxdept = 0, maxid = 0;
            for(i=1; i<=n; i++)
            {
                if(!use[i]&&dept[i]>maxdept)
                {
                    maxdept = dept[i];
                    maxid = i;
                }
            }
            if(maxdept<=k)break;
            ans++;
            insertone(maxid);
        }
        printf("%d\n",ans);
    }
    return 0;
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LA3902 Network (树上dfs)

题目链接:点击打开链接 题意:n台机器连成一个树状网络,其中叶节点是客户端,其他节点是服务器,目前有一台服务器s正在提供服务。让你在其他服务器上也安排同样的服务,使得每台客户端到最近服务器的距离不超过...

LA3902 Network

#include #include #include #include #include using namespace std; const int maxn=1010; vectorgr...

UVA 1267 && LA 3902 Network (思路--树上的DFS)

大体题意: 给你n 个节点的树,其中叶子节点是客户端,内部的节点是服务器!你需要把一些服务器变成VOD,使得每个客户端到最近VOD的距离不超过K,开始的时候,已经有了一个VOD ,标号是s(也是根节...

UVALive3902-Network(dfs)

题目链接http://vjudge.net/problem/UVALive-3902思路先无根树转有根树,然后可以贪心 对于深度大于k的节点,选择深度最深的节点,向上走k步走到节点A,那么将节点A作...
  • Lzedo
  • Lzedo
  • 2016年11月08日 16:38
  • 119

(06-08补)UVALive 3902 Network 网络

题目大意:n台机器连成一个树状网络,其中叶节点是客户端,其他节点是服务器。现在有一台服务器在节点s,服务器能传播的信号的距离为k,因为有的用户距离服务器的距离大于k,所以必须添加服务器。问最少要添加几...

算法竞赛入门经典训练指南 例题1-15 网络 Network UVALive 3902

题目:https://vjudge.net/problem/UVALive-3902思路:贪心 + 无根树转有根树 + 搜索(我用的BFS) 以初始服务器为根节点算出各个叶节点的深度排好序,从深度最...

UVALive - 3902 Network

Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu ...

UVALive 3902 Network

开始把没被根节点影响到的叶子节点存起来 然后每次找深度最大的 往上k层的祖先 然后从这个祖先 开始dfsk层 除掉这些叶子 就可以了 我当时是处理的这个祖先的子孙 WA了2发 其实也会向父节点方向去除...

LA3027 - Corporative Network,并查集

A very big corporation is developing its corporative network. In the beginning each of the Nenterpri...
  • yew1eb
  • yew1eb
  • 2013年03月26日 01:07
  • 1475

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意:             查询某一个节点到根节点之间的距离解题思路:            加权并查集问题。之前做的题目是“查看两个或多个节点是否在同一个集合下”,现在的题目是“查询某个节点...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LA3902 - Network
举报原因:
原因补充:

(最多只允许输入30个字)