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 ...
随机推荐
- Tomcat自动启动脚本
Tomcat自动启动脚本#!/bin/bash # chkconfig: 2345 10 90 # description: Starts and Stops the Tomcat daemon. T ...
- mybatis sql注入安全
1.mybatis语句 SELECT * FROM console_operator WHERE login_name=#{loginName} AND login_pwd=#{loginPwd} 2 ...
- linux 全自动提权 exp perl脚本
linux 全自动提权 exp perl脚本 作者: admin 日期: 2013/01/19发表评论 (0) 查看评论 国外流传过来的 地址 http://dl.packetstormsecur ...
- 在线API文档
http://www.ostools.net/apidocs A Ace akka2.0.2 Android Ant Apache CXF Apache HTTP服务器 ASM字节码操作 AutoCo ...
- 由浅入深了解Thrift之服务模型和序列化机制
一.Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎.其允许你定义一个简单的定义文件中的数据类型和服务接口.以作为输入文件,编 ...
- Web App中的Flexbox应用
虽然语法可能比较混杂,但 Flexbox 还是名不虚传的.它创造的是可伸缩的.有弹性的.可改变视觉顺序的智能盒子.它提供了简单的CSS布局方案范例让容器总是处于垂直水平居中的位置.使用盒模型来工作是非 ...
- POJ 2013
#include <iostream> #include <string> #define MAXN 20 using namespace std; string _m[MAX ...
- Java获取最后插入MySQL记录的自增ID值方法
方法一: String sql = "INSERT INTO users (username,password,email) VALUES (?,?,?);"; PreparedS ...
- IOS笔记 #pragma mark的用法和作用(方便查找和导航代码)
简单的来说就是为了方便查找和导航代码用的. 下面举例如何快速的定位到我已经标识过的代码. #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick{ ...
- BZOJ 1452: [JSOI2009]Count 二维树状数组
1452: [JSOI2009]Count Description Input Output Sample Input Sample Output 1 2 HINT Source 题解:设定C[101 ...