LA3902 Networlk
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
坑点:只需覆盖叶子节点即可,不用覆盖中间节点
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <algorithm>
- #include <queue>
- #include <vector>
- #define min(a, b) ((a) < (b) ? (a) : (b))
- #define max(a, b) ((a) > (b) ? (a) : (b))
- #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
- inline void swap(int &a, int &b)
- {
- int tmp = a;a = b;b = tmp;
- }
- inline void read(int &x)
- {
- x = ;char ch = getchar(), c = ch;
- while(ch < '' || ch > '') c = ch, ch = getchar();
- while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
- if(c == '-') x = -x;
- }
- const int INF = 0x3f3f3f3f;
- const int MAXN = + ;
- struct Edge
- {
- int u,v,nxt;
- Edge(int _u, int _v, int _nxt){u = _u;v = _v;nxt = _nxt;}
- Edge(){}
- }edge[MAXN << ];
- int head[MAXN], cnt, fa[MAXN], q[MAXN], deep[MAXN], n, t, s, k, b[MAXN], ans;
- inline void insert(int a, int b)
- {
- edge[++cnt] = Edge(a,b,head[a]);head[a] = cnt;
- }
- void bfs(int u)
- {
- int he = , ta = ;
- q[he] = s;deep[s] = ;fa[s] = ;
- while(he < ta)
- {
- int now = q[he ++];
- for(register int pos = head[now];pos;pos = edge[pos].nxt)
- {
- int v = edge[pos].v;
- if(v == fa[now]) continue;
- fa[v] = now;deep[v] = deep[now] + ;
- q[ta ++] = v;
- }
- }
- }
- void dfs(int u, int pre, int step)
- {
- if(u == )return;
- if(step > k) return;
- for(register int pos = head[u];pos;pos = edge[pos].nxt)
- {
- int v = edge[pos].v;
- if(v == pre)continue;
- b[v] = ;
- dfs(v, u, step + );
- }
- }
- int main()
- {
- read(t);
- for(;t;--t)
- {
- memset(head, , sizeof(head));cnt = ;ans = ;
- memset(edge, , sizeof(edge));memset(b, , sizeof(b));
- memset(deep, , sizeof(deep));memset(fa, , sizeof(fa));
- memset(q, , sizeof(q));
- read(n), read(s), read(k);
- for(register int i = ;i < n;++ i)
- {
- int tmp1,tmp2;
- read(tmp1), read(tmp2);
- insert(tmp1, tmp2);
- insert(tmp2, tmp1);
- }
- bfs(s);
- for(register int j = n;deep[q[j]] > k;-- j)
- if(!b[q[j]])
- {
- //是否是叶子
- int tmp = ;
- for(register int pos = head[q[j]];pos;pos = edge[pos].nxt)
- ++ tmp;
- if(tmp > ) continue;
- int tmp1 = k, tmp2 = q[j];
- while(fa[tmp2] && tmp1)
- {
- -- tmp1;
- tmp2 = fa[tmp2];
- }
- b[tmp2] = ;
- dfs(tmp2, -, );
- ++ ans;
- }
- printf("%d\n", ans);
- }
- return ;
- }
LA3902
LA3902 Networlk的更多相关文章
- LA3902 Network
给出一棵树,对于每一个叶子节点要使得在它的k距离内至少一个节点被打了标记,(叶节点不能打标记,非叶结点也不必满足这个条件),现在已经有一个节点s被打了标记,问至少还要打几个标记(这表达能力也是捉急.. ...
- LA3902 Network (树上dfs)
题目链接:点击打开链接 题意:n台机器连成一个树状网络,其中叶节点是客户端,其他节点是服务器,目前有一台服务器s正在提供服务.让你在其他服务器上也安排同样的服务,使得每台客户端到最近服务器的距离不超过 ...
- LA3902网络
题意: 给你一棵树,所有叶子节点都是客户端,其他的都是服务器,然后问你最少在多少个服务器上安装VOD能使所有的客户端都能流畅的看视频,流畅看视频的条件是每个客户端距离他最近的安装VOD的服务 ...
- UVALive3902 Network[贪心 DFS&&BFS]
UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to s ...
- 【树形贪心】【UVA1267】Network
重要意义:复习好久没写的邻接表了. Network, Seoul 2007, LA3902 Consider a tree network with n nodes where the interna ...
随机推荐
- VM 虚拟机使用桥接模式却连不上网的解决办法(转载)
只需将VM的虚拟网络编辑器中关于 VMnet0 的设置改一下就行了: 1.进入VMware的 编辑 -> 虚拟网络编辑器. 第一步 2.选择更改设置. 3.将VMnet0(或其它类型为桥接模式的 ...
- 02-python 学习第二天
今天学习了以下几个方面的内容,虽然部分内容不能理解,跟着老师写出了代码. 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 程序练习1:购物车程序 请闭眼写出以下程序. 程序: ...
- selenium基础(参数化脚本)
参数化脚本 什么是参数化 参数化就是用包含多组数据的参数列表,使之替换脚本中的响应常量值,这样,在脚本运行的时候,就会使用参数表中的数据来代替脚本中的常量值 由于参数表中包含了多组数据,所以执行用例时 ...
- 移植 Busybox
下载 busybox 从 http://www.busybox.net/downloads/busybox1.1.3.tar.gz/下载 busybox1.1.3 到/tmp 目录当中,并解压. ...
- CVE-2016-0095提权漏洞分析
1 前言 瞻仰了k0shl和鹏哥 的漏洞分析,感慨万千,任重而道远. 2 系统环境和工具 windows 7 32旗舰版 windbg 3 poc 3.1poc复现 首先k0shl大佬给出的poc() ...
- Atcoder arc096
C:Half and Half 几个if语句贪心算一算就好了 #include<cstdio> #include<algorithm> using namespace std; ...
- 解决mysql中无法修改事务隔离级别的问题
使用SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;修改数据库隔离级别, 然后执行SELECT @@TX_ISOLATION;后发现数据库的隔离级别并 ...
- Flink Checkpoint 问题排查实用指南
在 Flink 中,状态可靠性保证由 Checkpoint 支持,当作业出现 failover 的情况下,Flink 会从最近成功的 Checkpoint 恢复.在实际情况中,我们可能会遇到 Chec ...
- 0903NOIP模拟测试赛后总结
分-rank33.这次考试心态挂了. 拿到题目通读三道题,发现都十分恶心. 然后把时间押到了T1上.将近两个小时,打了个dfs,一直调调调. 最后没调出来,手模了个数据就把自己两个小时的思路hack了 ...
- 第十四章 Odoo 12开发之部署和维护生产实例
本文中将学习将 Odoo 服务器作为生产环境的基本准备.安装和维护服务器是一个复杂的话题,应该由专业人员完成.本文中所学习的不足以保证普通用户创建应对包含敏感数据和服务的健壮.安全环境. 本文旨在介绍 ...