uva 1267 Network(DFS)

                                 uva 1267 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(3技术分享n技术分享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



题目大意:给出n,s,k,即有n台客户端和服务器,然后给出n-1条边,连接n台主机,现在在序号为s的服务器上有一个资源,为了方便所有用户浏览(访问路径长度不超过k),要在较少的服务器上复制资源,问说最少再在几个服务器上复制资源

解题思路:首先现将无根图转化成树,以资源所在的服务器s为根,然后dfs建树,建树的过程中将所有叶子节点按照深度记录。其二就是枚举需要复制资源的服务器,从节点深度最大的叶子节点开始,向上查找父亲节点k次,即为当前的最有策略,然后将距离该服务器不超过k的终端全部标记



#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
int n, s, k, fa[1005], vis[1005];
vector<int> gra[1005], node[1005];
void Tra(int u, int f, int d) {//转换为树
	fa[u] = f;
	int n = gra[u].size();
	if (n == 1 && d > k) node[d].push_back(u);
	for (int i = 0; i < n; i++) {
		int v = gra[u][i];
		if (v != f) Tra(v, u, d + 1);
	}
}
void DFS(int u, int f, int d) { 
	vis[u] = 1;
	int n = gra[u].size();
	for (int i = 0; i < n; i++) {
		int v = gra[u][i];
		if (v != f && d < k) DFS(v, u, d + 1); //距离不超过k
	}
}
int solve() {
	int ans = 0;
	memset(vis, 0, sizeof(vis));
	for (int d = n - 1; d > k; d--) {
		for (int i = 0; i < node[d].size(); i++) {
			int u = node[d][i];
			if (vis[u]) continue;
			int v = u;
			for (int j = 0; j < k; j++) v = fa[v];//v是u的k级祖先,所以在v处放服务器,是最优的
			DFS(v, -1, 0);
			ans++;
		}
	}
	return ans;
}
int main() {
	int	T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d\n%d %d", &n, &s, &k);
		for (int i = 1; i <= n; i++) {
			gra[i].clear();
			node[i].clear();
		}	
		int a, b;
		for (int i = 0; i < n - 1; i++) {
			scanf("%d %d", &a, &b);
			gra[a].push_back(b);
			gra[b].push_back(a);
		}
		Tra(s, -1, 0);
		printf("%d\n", solve());
	}
	return 0;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。