【Jason's_ACM_解题报告】Network

本文介绍了如何在树形网络中确保服务质量,即每个客户端到VOD服务器的距离不超过k。通过建立最小数量的VOD系统副本,确保每个客户都能在限制距离内访问服务。给出了一种贪心策略,选择深度最大的节点的k级祖先作为副本位置。题目提供了示例输入和输出,展示了计算所需最小副本数的方法。
摘要由CSDN通过智能技术生成

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 Swhich 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

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


0


此题为一道图论题,通过这道题,算是唤醒了我原来对于图论这一部分知识点的记忆,通过学习Liu的代码,我发现了C++带给程序员的便利与直观是远远大于Pascal的,虽然曾经挚爱Pascal。

学会了c语言存储树结构的方式。摆脱了Pascal数组模拟指针的影子。

题目的解决运用了贪心的思想,即对于深度最大的点u,选择u的k级祖先是最划算的(父亲为1级,父亲的父亲为2级...)。

1.若选择x级祖先(x>k),很显然,若要满足服务到点u,必须在选一个点作为replica,不划算。

2.若选择x级祖先(x<k),假设有p个clients点与u点的距离为d,则此时添加了新的replica后,replica距u点距离为x<k,而replica距其余p个clients点距离为dist=(d-x),x若减小则dist增大,对于其余p个点来说不划算。

证毕。


附代码如下:

#include<cstdio>
#include<vector>
#include<cstring>

using namespace std;

#define MAXN (1000+5)
#define clr(x) (memset(x,0,sizeof(x)))

vector<int> tr[MAXN],nodes[MAXN];

int n,svr,dis;
int fa[MAXN];
bool cvr[MAXN];

void dfs(int u,int f,int d){
	fa[u]=f;
	int k=tr[u].size();
	if(k==1&&d>dis)nodes[d].push_back(u);
	for(int i=0;i<k;i++){
		int v=tr[u][i];
		if(v!=f)dfs(v,u,d+1);
	}
}

void dfs2(int u,int f,int d){
	cvr[u]=true;
	int k=tr[u].size();
	for(int i=0;i<k;i++){
		int v=tr[u][i];
		if(v!=f&&d<dis)dfs2(v,u,d+1);
	}
}

int solve(){
	int ret=0;
	clr(cvr);
	for(int d=n-1;d>dis;d--){
		int k=nodes[d].size();
		for(int i=0;i<k;i++){
			int v=nodes[d][i];
			if(cvr[v])continue;
			for(int j=0;j<dis;j++)v=fa[v];
			dfs2(v,-1,0);
			ret++;
		}
	}
	return ret;
}

int main(){
//freopen("in.txt","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--){	
		scanf("%d",&n);
		scanf("%d%d",&svr,&dis);
		for(int i=1;i<=n;i++){
			tr[i].clear();
			nodes[i].clear();
		}
		for(int i=0;i<n-1;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			tr[u].push_back(v);
			tr[v].push_back(u);
		}
		dfs(svr,-1,0);
		int ans;
		ans=solve();
		printf("%d\n",ans);
		
	}
//fclose(stdin);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值