1267 - Network
Time limit: 3.000 seconds
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 ton . Among the servers, there is an original serverS which provides VOD (Video On Demand) service. To ensure the quality of service for the clients, the distance from each client to the VOD serverS should not exceed a certain value k . The distance from a node u to a nodev in the tree is defined to be the number of edges on the path fromu to v . If there is a nonempty subsetC of clients such that the distance from each u in C to S is greater thank , 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) isk or less.
Given a tree network, a server S which has VOD system, and a positive integerk , find the minimum number of replicas necessary so that each client is within distancek from the nearest server which has the original VOD system or its replica.
For example, consider the following tree network.
![](https://icpcarchive.ecs.baylor.edu/external/39/p3902.png)
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 integern (3n
1, 000) which is the number of nodes of the tree network. The next line contains two integerss (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 followingn - 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
题目大意:给一树型网络,初始一个点s放置一台服务器,给一个k。先要求放置最少的服务器,使得所有叶子节点距离服务器距离<=k。输出k。
思路:先转换成s为根的树,根据观察即可发现策略:从最深的叶子节点开始考虑,每次放在距离最远的没有覆盖的叶子节点k的节点,每放置一个服务器进行一次dfs。
这里用一个深度表node来保存深度:vector<int> node[maxn]; node[d] 就是深度为d的叶子节点。
完整代码:
/*0.019s*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
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;///计算fa数组
int nc = gr[u].size();
if (nc == 1 && d > k) ///度为1的就是叶节点了
nodes[d].push_back(u);///根据深度把叶子结点插入nodes表里
for (int i = 0; i < nc; i++)
{
int v = gr[u][i];
if (v != f) dfs(v, u, d + 1);
}
}
void dfs2(int u, int f, int d)
{
covered[u] = true;
int nc = gr[u].size();
for (int i = 0; i < nc; i++)
{
int v = gr[u][i];
if (v != f && d < k) dfs2(v, u, d + 1); /// 只覆盖到新服务器距离不超过k的结点
}
}
int solve()
{
int ans = 0;
memset(covered, 0, sizeof(covered));
for (int d = n - 1; d > k; d--)
for (int i = 0; i < nodes[d].size(); i++) /// 遍历所有叶节点
{
int u = nodes[d][i];
if (covered[u]) continue; /// 不考虑已覆盖的结点
int v = u;
for (int j = 0; j < k; j++) v = fa[v]; /// v是u的k级祖先
dfs2(v, -1, 0); /// 在结点v放服务器
ans++;
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &s, &k);
for (int i = 1; i <= n; i++)
{
gr[i].clear();
nodes[i].clear();
}
for (int i = 0; i < n - 1; i++)
{
int a, b;
scanf("%d%d", &a, &b);
gr[a].push_back(b);///邻接表
gr[b].push_back(a);
}
dfs(s, -1, 0);///预处理
printf("%d\n", solve());
}
return 0;
}