# LA3902 - Network

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.

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 (3n1, 000) which is the number of nodes of the tree network. The next line contains two integers s (1sn) and k (k1) 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.

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;
}

• 本文已收录于以下专栏：

## UVA LA3902-network(树形dp)

Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal...
• u012350533
• 2014年01月13日 21:51
• 1100

## LA3902 Network

#include #include #include #include #include using namespace std; const int maxn=1010; vectorgr...
• q295657451
• 2014年02月17日 19:47
• 477

## UVALive - 3902 Network 无根树转为有跟树

• L123012013048
• 2015年02月24日 14:43
• 485

## LA3902 Network (树上dfs)

• Kirito_Acmer
• 2016年03月30日 12:42
• 416

## NetWork(LA 3902)深度优先搜索

• qq_33929112
• 2016年10月13日 21:18
• 190

## LA3902网络

• u013761036
• 2014年11月21日 16:19
• 350

## LA3902

• vv494049661
• 2016年01月23日 17:36
• 228

## 网络 network,Seoul 2007 LA3902 解题感想

• qq_21970857
• 2015年02月01日 17:06
• 727

## Network In Network(精读)

• shengno1
• 2014年11月20日 15:30
• 9478

## 深度学习（二十六）Network In Network学习笔记

• hjimce
• 2016年01月04日 20:23
• 22851

举报原因： 您举报文章：LA3902 - Network 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)