Network

来源:互联网 发布:egd网络黄金最新报价 编辑:程序博客网 时间:2024/05/16 18:24

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

\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 integern (3$ \le$n$ \le$1, 000) which is the number of nodes of the tree network. The next line contains two integerss (1$ \le$s$ \le$n) and k (k$ \ge$1) wheres 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
这是一道关于树的题,我之前没有做过这方面的题,所以通过这道题学到了不少东西。首先,树的存储,树的每个结点都与一个或多个结点相连,构成多条边。用vector<int> edge【maxn】来存储结点,每个vector存储与这个结点相邻的结点,很显然这样存储的话与该结点相邻的结点既有可能是它的子结点也有可能是其父结点,这一点要特别注意。其次,树中非常重要的一点就是关于树的深度优先搜索(dfs)。深搜遍历了树的所有结点,对于关于树的问题的求解是必不可少的。关于深搜我是这样理解的:我们从某一结点出发,遍历它的子节点,但是不是一次全部遍历完,而是从它的第一个子结点开始遍历子结点的子结点,知道再无子结点,然后再遍历上一层的第二个子结点,以此类推。回到这道题,我们需要在合适的结点处放置服务器,使得所有叶子结点到服务器的距离小于某一特定值k。试想一下,如果我们把第一台服务器所在的结点当作根结点,然后我们dfs存储所有未被覆盖的叶子结点,在距离深度最大的叶子结点k处放置一台服务器,然后删除加上这台服务器后被覆盖的结点,以此类推,就能求解出最终答案。代码如下:

#include<iostream>#include<cstdio>#include<vector>#include<cstring>using namespace std;const int maxn=1000+5;int k,n;vector<int> edge[maxn],Nobe[maxn];int fa[maxn];bool covered[maxn];void dfs(int u,int f,int d){fa[u]=f;if(edge[u].size()==1&&d>k) Nobe[d].push_back(u);for(int i=0;i<edge[u].size();i++){int v=edge[u][i];if(v!=f) dfs(v,u,d+1);}}void dfs1(int u,int f,int d){for(int i=0;i<edge[u].size();i++){int v=edge[u][i];if(d<k&&v!=f){covered[v]=true;dfs1(v,u,d+1);}}}int check(){int ans=0;memset(covered,0,sizeof(covered));for(int i=n-1;i>k;i--)for(int j=0;j<Nobe[i].size();j++){int u=Nobe[i][j];if(covered[u]) continue;int k1=k;while(k1--) u=fa[u];dfs1(u,-1,0);ans++;}return ans;}int main(){int T;cin>>T;while(T--){int s,a,b;scanf("%d%d%d",&n,&s,&k);for(int i=1;i<=n;i++){edge[i].clear();Nobe[i].clear();}for(int i=0;i<n-1;i++){scanf("%d%d",&a,&b);edge[a].push_back(b);edge[b].push_back(a);}dfs(s,-1,0);printf("%d\n",check());}return 0;}


0 0
原创粉丝点击