LA 3902 Network
人生第一道图论题啊,有木有
题意:
有一个树状网络,有一个原始服务器s,它的服务范围是k
问至少再放多少台服务范围是k的服务器才能使网络中的每个节点都被覆盖掉
解法:
我们以原始服务器为根将其转化成一个有根树,则深度不超过k的节点都已经被原始服务器覆盖。
我们选择深度最大的节点u然后将它的k级祖先设为服务器,进行一次DFS,将所有距离它不超过k的节点覆盖。
表示:
图的表示在这里面是用邻接表来表示的,如果a、b两个节点相邻,则gr[a]中放入b,gr[b]中放入a
怎样才算转化为有根树了?那就是把每个节点的爸爸(father)算出来,记录在fa数组中
试想,如果深度大于k的节点都已被覆盖,那么其他非叶子节点也一顶被覆盖了
所以将深度大于k的节点放在nodes表中
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = + ;
vector<int> gr[maxn], nodes[maxn];
int n, s, k, fa[maxn];
bool coverd[maxn]; void DFS(int u, int f, int d)
{//计算各个节点的祖先,放在fa数组中
fa[u] = f;
int nc = gr[u].size();
if(nc == && d > k)
nodes[d].push_back(u);
for(int i = ; i < nc; ++i)
{
int v = gr[u][i];
if(v != f)
DFS(v, u, d + );
}
} void DFS2(int u, int f, int d)
{
coverd[u] = true;
int nc = gr[u].size();
for(int i = ; i < nc; ++i)
{
int v = gr[u][i];
if(v != f && d < k)
DFS2(v, u, d + );
}
} int solve(void)
{
int ans = ;
memset(coverd, false, sizeof(coverd));
for(int d = n - ; d > k; --d)
for(int i = ; i < nodes[d].size(); ++i)
{
int u = nodes[d][i];
if(coverd[u]) continue; int v = u;
for(int i = ; i < k; ++i) //v是u的k级祖先
v = fa[v];
DFS2(v, -, );
++ans;
}
return ans;
} int main(void)
{
#ifdef LOCAL
freopen("3902in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n, &s, &k);
for(int i = ; i <= n; ++i)
{
gr[i].clear();
nodes[i].clear();
}
for(int i = ; i < n - ; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
gr[a].push_back(b);
gr[b].push_back(a);
}
DFS(s, -, );
printf("%d\n", solve());
}
return ;
}
代码君
LA 3902 Network的更多相关文章
- LA 3902 Network(树上最优化 贪心)
Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...
- Uva LA 3902 - Network 树形DP 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- Uva 网络(Network,Seoul 2007,LA 3902)
#include<iostream> #include<cstring> #include<vector> using namespace std; +; int ...
- UVALive 3902 Network (树+dfs)
Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal ...
- [UVALive 3902] Network
图片加载可能有点慢,请跳过题面先看题解,谢谢 一道简单的贪心题,而且根节点已经给你了(\(S\)),这就很好做了. 显然,深度小于等于 \(k\) 的都不用管了(\(S\) 深度为0),那么我们只需要 ...
- LA 3902 网络
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVaLive 3902 Network (无根树转有根树,贪心)
题意:一个树形网络,叶子是客户端,其他的是服务器.现在只有一台服务器提供服务,使得不超k的客户端流畅,但是其他的就不行了, 现在要在其他结点上安装服务器,使得所有的客户端都能流畅,问最少要几台. 析: ...
- Uva 3902 Network
题目大意: 在非叶子节点上安装最少的服务器使得,每个叶子节点到服务器的距离不超过k. 贪心+图上的dfs. 先从深度最大的叶子节点开始找.找到父节点后再用这个父节点进行扩充. /* ********* ...
- UVALive3902 Network[贪心 DFS&&BFS]
UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to s ...
随机推荐
- Asp.net的服务器推技术 (Server Push)
在以往的和服务器端通信技术中,我们多数使用的是AJAX轮询式访问,也就是在Javascript中控制时间间隔,然后每隔一段时间就访问一次服务器,然后获得数据或通知.但是这种轮询方式的访问有90%是在做 ...
- eclipse下使用API操作HDFS
1)使用eclipse,在HDFS上创建新目录 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...
- hdu5017 Ellipsoid(旋转)
比赛的时候跳进这个大坑里,最后代码是写出来了.看到好像很多都是模拟退火做的,下面提供一个奇怪的思路吧. ax^2+by^2+cz^2+dyz+exz+fxy=1(*) 通过一些奇特的YY我们可以知道这 ...
- 【linux】文字提取
提取IP地址: 方法①: ifconfig eth3|grep Bcast|cut -d ":" -f2|cut -d " " -f1 ifconfig: 显示 ...
- iOS-xib(自定义UITableViewCell)
1.创建一个自定义的xib
- Linux静态库和动态库
Linux 工具 ❑ GCC: The GNU Compiler Collection, containing the GNU C compiler❑ G++: A C++ compiler, inc ...
- java 哪些情况下会使对象锁释放
Java_多线程_锁释放 问:Java多线程运行环境中,在哪些情况下会使对象锁释放?答:由于等待一个锁的线程只有在获得这把锁之后,才能恢复运行,所以让持有锁的线程在不再需要锁的时候及时释放锁是很重要的 ...
- Qt通过UDP传图片,实现自定义分包和组包
一.包头结构体 //包头 struct PackageHeader { //包头大小(sizeof(PackageHeader)) unsigned int uTransPackageHdrSize; ...
- inno setup判断是Windows系统版本(其实还是Delphi代码,还能检查域控制器和家庭版)
1.设置Windows最低版本要求 [Setup]: MinVersion 格式: a.bb,c.dd,这里 a.bb 是 Windows 版本,c.dd 是 Windows NT 版本. 默认值: ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...