题意:一个树形网络,叶子是客户端,其他的是服务器。现在只有一台服务器提供服务,使得不超k的客户端流畅,但是其他的就不行了,

现在要在其他结点上安装服务器,使得所有的客户端都能流畅,问最少要几台。

析:首先这是一棵无根树,我们可以转成有根树,正好可以用原来的那台服务器当根,然后在不超过 k 的叶子结点,就可以不用考虑,

然后要想最少,那么就尽量让服务器覆盖尽量多的叶子,我们可以从最低的叶子结点开始向上查找最长的距离当服务器,这样是最优的。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> G[maxn], node[maxn];
int f[maxn];
bool cover[maxn]; void dfs(int u, int fa, int cnt){
f[u] = fa;
if(1 == G[u].size() && cnt > m) node[cnt].push_back(u);
for(int i = 0; i < G[u].size(); ++i) if(G[u][i] != fa)
dfs(G[u][i], u, cnt+1);
} void dfs2(int u, int fa, int cnt){
cover[u] = true;
if(cnt >= m) return ;
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(v == fa) continue;
dfs2(v, u, cnt+1);
}
} int solve(){
memset(cover, false, sizeof cover);
int ans = 0;
for(int j = n-1; j > m; --j)
for(int i = 0; i < node[j].size(); ++i){
int v = node[j][i];
if(cover[v]) continue; for(int k = 0; k < m; ++k) v = f[v];
dfs2(v, -1, 0);
++ans;
}
return ans;
} int main(){
int T; cin >> T;
while(T--){
int s;
scanf("%d %d %d", &n, &s, &m);
for(int i = 1; i <= n; ++i) G[i].clear(), node[i].clear();
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(s, -1, 0);
printf("%d\n", solve());
}
return 0;
}

UVaLive 3902 Network (无根树转有根树,贪心)的更多相关文章

  1. LA 3902 Network(树上最优化 贪心)

    Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...

  2. [UVALive 3902] Network

    图片加载可能有点慢,请跳过题面先看题解,谢谢 一道简单的贪心题,而且根节点已经给你了(\(S\)),这就很好做了. 显然,深度小于等于 \(k\) 的都不用管了(\(S\) 深度为0),那么我们只需要 ...

  3. UVALive 3902 Network (树+dfs)

    Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal ...

  4. 树形dp|无根树转有根树|2015年蓝桥杯生命之树

    2015年蓝桥杯第十题--生命之树(无根树dfs) ①暴力解法:枚举子集(选点) + dfs判断连通性(题目要求连通)满足上面两个条件下找出最大值权值和 ②dfs无根树转有根树,递归找最优 先学习无根 ...

  5. 再谈树---无根树转有根树( dfs搜索转化+fa数组记录父节点) *【模板】

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <vector> ...

  6. 无根树转有根树(dfs,tree)

    #include <bits/stdc++.h> #include <iostream> #include <queue> #include <stdio.h ...

  7. Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  8. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  9. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

随机推荐

  1. java gc小结

    java的内存结构: 1. 堆: java所有通过new新建的对象都是在堆上进行分配的; 根据不同的垃圾回收算法, 堆的结构也不相同, 如果采用的是分代垃圾回收, 那么堆就分为年轻代和年老代两部分. ...

  2. php返回HTTP状态码

    HTTP协议状态码,调用函数时候只需要将$num赋予一个下表中的已知值就直接会返回状态了.<?PHP /** * HTTP Protocol defined status codes* HTTP ...

  3. celery模块

    Celery Celery是管理分布式任务队列的工具,其本身并不是任务队列. Celery常见概念有brokers.backend.workers.tasks. brokers:中间人,是Celery ...

  4. EasyDarwin云平台:EasyCamera开源摄像机接入海康威视摄像机PS流转ES流

    本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376 海康威视使用PS流封装H.264流,EasyDarwin云平台支持ES流.当我们 ...

  5. EasyDarwin开源音频解码项目EasyAudioDecoder:基于ffmpeg的安卓音频(AAC、G726)解码库(第一部分,ffmpeg-android的编译)

    ffmpeg是一套开源的,完整的流媒体解决方案.基于它可以很轻松构建一些强大的应用程序.对于流媒体这个行业,ffmpeg就像圣经一样的存在.为了表达敬意,在这里把ffmpeg官网的一段简介搬过来,ff ...

  6. EasyDarwin流媒体云平台架构

    EasyDarwin目前正在做的开源流媒体云平台架构:

  7. 通过WindowManager图片切换的效果

    最近为这个事情焦头烂额,原因无他.原来打算是把ViewPager放在WindowManager中,再设定一个定时器,让图片自动切换,但是搞了很久,发现无论如何,这个图片只显示一张.虽然日志看得出来图片 ...

  8. Linux就该这么学--命令集合2(系统状态检测命令)

    1.查看本机当前的网卡配置与网络状态等信息:(ifconfig [网络设备] [参数]) ifconfig 2.查看系统的内核名称.内核发行版.内核版本.节点名.硬件名称.硬件平台.处理器类型.操作系 ...

  9. Mongoose学习(2)

    1.Schema的扩展 UserSchema.methods.findUserName = function(cb){ return this.model('user').find({username ...

  10. 操作系统:Bochs 2.6.8的配置文件bochsrc.bxrc修改

    由于现在Bochs 2.6.8相比之前有些改动,之前的配置文件不能直接运行,针对配置文件需要有些修改. 1. 配置文件 ######################################## ...