一颗树中一个节点能覆盖到距离该节点k以内的叶子节点
现在已有一个节点安放了装置,问最少再加几个能覆盖所有叶子节点
首先dfs一边,吧已经安放了装置的节点当做root,记录每一个距离root k以上点的叶子节点。
然后对于每一个未被覆盖的叶子节点我们进行第二遍dfs,根据第一遍dfs中记录的father情况,先向上寻找祖先。(为了不让覆盖的距离浪费)然后再从找到的祖先开始覆盖周围距离k以内的叶子节点,
最后统计一共加了多少个节点,
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

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 (3n
1, 000) which is the number of nodes of the tree network. The next line contains two integers s(1
s
n) and k (k
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<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 1100
int vis[MAXN],cvr[MAXN],f[MAXN],n,s,k;
vector<int> leaf[MAXN]; //用于对叶子分层
vector<int> link[MAXN]; //邻接表
#define add(x,u,v) (x[u].push_back(v))
#define pb(x) push_back(x)
void dfs(int u,int dist){
bool flag=0;
vis[u]=1;
for(int i=0;i<link[u].size();i++){
int v=link[u][i];
if(vis[v]) continue;
flag=1;f[v]=u;
dfs(v,dist+1);
}
if(dist>k && !flag)
leaf[dist].pb(u);
}
void dfs1(int u,int rest){
if(rest<0) return;
cvr[u]=1;
vis[u]=1;
for(int i=0;i<link[u].size();i++){
int v=link[u][i];
if(vis[v]) continue;
dfs1(v,rest-1);
}
}
int main(){
int cs;
scanf("%d",&cs);
for(int css=1;css<=cs;css++){
scanf("%d%d%d",&n,&s,&k);
for(int i=0;i<=n;i++) link[i].clear(),leaf[i].clear();
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
add(link,u,v);
add(link,v,u);
}
memset(vis,0,sizeof(vis));f[s]=0;
dfs(s,0);
int ans=0;
memset(cvr,0,sizeof(cvr));
for(int i=n;i>k;i--){
for(int j=0;j<leaf[i].size();j++){
int l=leaf[i][j];
if(cvr[l]) continue;
int ct=l;
for(int m=k;m>0;m--) ct=f[ct];
memset(vis,0,sizeof(vis));
dfs1(ct,k);
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}