Network

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

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 户户通没有信号怎么办视频 雪花泥粘衣服上怎么办 遗产按份额处分判决后怎么办 宝马后驱车下雪天怎么办 宝马后驱车路滑怎么办 车子陷入泥地里怎么办 深圳居住证签注过期了怎么办 手机不能播放视频乱码了怎么办 被加密的视频无法观看怎么办 苹果5忘记id密码怎么办 七个月宝宝脾胃不好怎么办 八个月宝宝脾虚怎么办 七个月宝宝脾胃虚怎么办 海岛奇兵点错了怎么办 螳螂的脚断了怎么办 海岛奇兵打不过玩家怎么办 海岛奇兵资源满了怎么办 海岛奇兵杯越来越多打不玩家怎么办 海岛奇兵控杯技巧 杯数太高怎么办 海岛奇兵发现求救信号怎么办 海岛奇兵被打了怎么办 小鱼翅卡喉咙了怎么办 鱼翅卡在喉咙里怎么办 斗鱼身份证被使用怎么办 做的鱼丸太腥了怎么办 做鱼丸太稀了怎么办 斗鱼手机号换了怎么办 斗鱼直播掉帧怎么办 手机一直卡顿点不动怎么办呢 斗鱼直播分值底怎么办 斗鱼6000鱼丸怎么办卡 斗鱼直播没人看怎么办 淘宝直播间没人气怎么办 挂水了还是有热度怎么办 陌陌工会不结算工资怎么办 滴滴给了差评怎么办 饿了么星级低怎么办 滴滴乘客给低星怎么办 蘑菇街自动收货前还没到怎么办 小主播人气太少别人看不到怎么办 税收分类编码不可用怎么办