UVaLive 3902 Network (无根树转有根树,贪心)
题意:一个树形网络,叶子是客户端,其他的是服务器。现在只有一台服务器提供服务,使得不超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 (无根树转有根树,贪心)的更多相关文章
- LA 3902 Network(树上最优化 贪心)
Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...
- [UVALive 3902] Network
图片加载可能有点慢,请跳过题面先看题解,谢谢 一道简单的贪心题,而且根节点已经给你了(\(S\)),这就很好做了. 显然,深度小于等于 \(k\) 的都不用管了(\(S\) 深度为0),那么我们只需要 ...
- UVALive 3902 Network (树+dfs)
Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal ...
- 树形dp|无根树转有根树|2015年蓝桥杯生命之树
2015年蓝桥杯第十题--生命之树(无根树dfs) ①暴力解法:枚举子集(选点) + dfs判断连通性(题目要求连通)满足上面两个条件下找出最大值权值和 ②dfs无根树转有根树,递归找最优 先学习无根 ...
- 再谈树---无根树转有根树( dfs搜索转化+fa数组记录父节点) *【模板】
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <vector> ...
- 无根树转有根树(dfs,tree)
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stdio.h ...
- 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 ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- 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. ...
随机推荐
- 【程序猿联盟】官网上线啦!coderunity.com
wx_fmt=jpeg" alt="" style="max-width:100%; height:auto!important"> 内容简单介 ...
- 深度解析开发项目之 02 - 使用VTMagic实现左右滑动的列表页
深度解析开发项目之 02 - 使用VTMagic实现左右滑动的列表页 实现效果: 01 - 导入头文件 02 - 遵守代理协议 03 - 声明控制器的属性 04 - 设置声明属性的frame 05 - ...
- php pack()函数详解与示例
pack和unpack在一般的程序中还真的不容易见到,但是如果你用过很久以前的php生成excel你就会知道了.他的excel的头就是pack出来的最近在尝试与C交互的时候又用上了这玩意,所以不得不再 ...
- 富文本编辑器 - RichEditor
基本功能 RichEditor 是一个继承自 WebView 的自己定义 view,枚举类型 Type 定了它所支持的排版格式: public enum Type { BOLD, ITALIC, SU ...
- IOS8 TouchID使用介绍
本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123 IOS8将指纹识别技术开放出来了.我们能够利用用户设置的touch I ...
- EasyDarwin幼教云视频平台在幼教平台领域大放异彩!
在之前的一篇方案<基于EasyDarwin云视频平台的幼儿园视频直播(手机直播/微信直播)解决方案>中,我们提到一种可以广泛应用于幼教.工厂.建筑工地以及各种现场监控的云视频平台方案,这种 ...
- CSS Overflow 属性清除浮动
清除浮动 设置overflow的一个更流行的用处是,说也奇怪,清除浮动.设置overflow并不会在该元素上清除浮动,它将清除自己(self-clear).意思就是,应用了overflow(auto或 ...
- 棋盘覆盖问题 (粉书 P230 【递归】** )
转载自:http://blog.csdn.net/akof1314/article/details/5423608 (赞) 在一个 2^k * 2^k 个方格组成的棋盘中,若恰有一个方格与其它方格不 ...
- sdut oj 1163 C语言实验——排列 (当初不会递归生成排列,这个题目现在才补上 刘汝佳给出了写法 *【模板】 当然有生成全排列的函数存在 )
C语言实验——排列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 有4个互不相同的数字,请按序输出由其中三个不重复数字组成的排列 ...
- MySQL学习笔记(四)——分组数据group by子句和having子句
分组允许把数据分为多个逻辑组,以便对每个组进行聚集计算. 例如我们查下每个系里有多少名学生: 在使用group by 子句之前,还需要知道一些规定: 1. group by 子句可以包含任意数目的列, ...