博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA3902 - Network (二次搜索+邻接表)
阅读量:5885 次
发布时间:2019-06-19

本文共 4110 字,大约阅读时间需要 13 分钟。

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

 

 

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

 

 

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.

 

 

 

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

 

 

 

1 0

 

题意:

找出所有的深度大于k的叶子节点,在每个服务器可以作用个距离的情况下,问放置最少多少的服务器使得所有的叶子节点都可以被覆盖到服务器的服务范围内

思路:

优先覆盖深度大的叶子节点

首先通过邻接表来记录节点之间的连接情况

先搜索第一遍,找出所有的叶子节点并将其放入优先队列,并对所有深度<=k的点作出标记can[point]=1

再对优先队列中的元素进行按照深度从大到小的顺序进行遍历,对每一个can[point]==0元素回溯k级父节点进行下述的dfs操作,并让ans++

以传入的父节点为中心对外进行遍历所有与他距离小于等于k的节点作上标记can[point]=1

具体实现请看代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;int tot;int b[2005],e[2005];int first[2005],nxt[2005];int d[1005];void build(int from,int to)//创建邻接表{ b[tot]=from; e[tot]=to; nxt[tot]=first[from]; first[from]=tot++;}struct node{ int point; int depth; friend bool operator<(node a,node b) { return a.depth
q;priority_queue
pq;int flag[1005][1005],can[1005];int father[1005];void bfs(int point0,int mod);int ans;int vis[1005];int n;int find_fa(int a)//回溯k级父节点{ int i=k; while(i--) a=father[a]; return a;}void dfs(int p,int d)//添加服务器,并对他周围的点进行更新{ if(d>k) return; vis[p]=1; can[p]=1; for(int i=first[p];i!=-1;i=nxt[i]) { if(vis[e[i]]==0) { dfs(e[i],d+1); } } }void leaf_back()//对所有的叶子节点按照深度从大到小的顺序遍历{ while(!pq.empty()) { node u=pq.top(); pq.pop(); int p=u.point; int p2=find_fa(p); if(!can[p]) { //cout<<" "<
<<' '<
<
>t; while(t--) { tot=0; ans=0; memset(first,-1,sizeof(first)); memset(nxt,0,sizeof(nxt)); memset(can,0,sizeof(can)); int begin; cin>>n>>begin>>k; for(int i=0;i
>h>>j; build(h,j); build(j,h); } //cout<

 

转载于:https://www.cnblogs.com/fly-white/p/10092782.html

你可能感兴趣的文章
uC/OS-II源码分析(三)
查看>>
什么是SEM和付费搜索营销?
查看>>
Linux下安装Tomcat 8.0.53
查看>>
10月12日云栖精选夜读 | 程序员工资那么高,为什么会找不到女朋友?
查看>>
轻松搞定RabbitMQ3:消息应答与消息持久化
查看>>
快速下载android源码
查看>>
Linux基础命令---cal
查看>>
Linux下文件重命名、创建、删除、修改及保存文件
查看>>
跟我学习php文件和目录常用函数-上篇
查看>>
关于存量和增量的杂谈
查看>>
JavaScript 单例模式
查看>>
PHP项目学习2
查看>>
Linux Shell常用技巧(目录)
查看>>
在微信网页版下用Chrome控制台发送消息
查看>>
CUBA CLI 2.1.0 发布,企业级应用开发平台
查看>>
lecture 3.1 递归和对象
查看>>
GPS轨迹数据集免费下载资源整理
查看>>
bulk更新mongodb的脚本
查看>>
通用组件示例
查看>>
Flutter 03: 图解第一个程序 Hello World!
查看>>