人生第一道图论题啊,有木有

题意:

有一个树状网络,有一个原始服务器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的更多相关文章

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

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

  2. Uva LA 3902 - Network 树形DP 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  3. Uva 网络(Network,Seoul 2007,LA 3902)

    #include<iostream> #include<cstring> #include<vector> using namespace std; +; int ...

  4. UVALive 3902 Network (树+dfs)

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

  5. [UVALive 3902] Network

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

  6. LA 3902 网络

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

  7. UVaLive 3902 Network (无根树转有根树,贪心)

    题意:一个树形网络,叶子是客户端,其他的是服务器.现在只有一台服务器提供服务,使得不超k的客户端流畅,但是其他的就不行了, 现在要在其他结点上安装服务器,使得所有的客户端都能流畅,问最少要几台. 析: ...

  8. Uva 3902 Network

    题目大意: 在非叶子节点上安装最少的服务器使得,每个叶子节点到服务器的距离不超过k. 贪心+图上的dfs. 先从深度最大的叶子节点开始找.找到父节点后再用这个父节点进行扩充. /* ********* ...

  9. UVALive3902 Network[贪心 DFS&&BFS]

    UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to s ...

随机推荐

  1. css 内联元素inline 行框全解

    首先看一篇文章: CSS框模型:一切皆为框 — 从行框说起 一 行框 看图说话 上图代表了框模型中的行框.line-height 属性设置行间的距离(行高).该属性会影响行框的布局.在应用到一个块级元 ...

  2. 数据分页 THINKPHP3.2 分页 三种分页方法

    数据分页 复制本页链接 opensns 通常在数据查询后都会对数据集进行分页操作,ThinkPHP也提供了分页类来对数据分页提供支持. 下面是数据分页的两种示例. 第一种:利用Page类和limit方 ...

  3. DevSecOps 简介(一)

    DevOps,或者说企业应用开发团队和系统运营团队的合作,已经成为一个时髦的 IT 话题.这一新的运营模式往往与敏捷式软件开发方法并举,同时还会利用云计算的可扩展性--这一切,都是为了使企业更加灵活, ...

  4. 在 Java EE 组件中使用 Camel Routes

    摘要:你可以通过集成 Camel 和 WildFly 应用服务器(使用 WildFly-Camel 子系统)在 Java EE 组件中开始使用 Apache Camel Routes. [编者按]作者 ...

  5. 【二】php常用方法

    -------------------------------数据类型------------------------------------------ 1.settype(var,type)  类 ...

  6. codeforces 439C Devu and Partitioning of the Array(烦死人的多情况的模拟)

    题目 //这是一道有n多情况的烦死人的让我错了n遍的模拟题 #include<iostream> #include<algorithm> #include<stdio.h ...

  7. POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)

    网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...

  8. swift-基础部分

    变量常量,注释,分号,整数,浮点数.数值行类型转换,类型别名,波尔值,元组,可选,断言              let binaryInteger = 0b10001  let twoThousan ...

  9. POJ 1317

    #include <iostream> #include <string> using namespace std; char p_code[] = {'_','a','b', ...

  10. SQL技术内幕-6 rank()over(order by XX COLLATE) 的用法

    DECLARE @Names TABLE ( name VARCHAR(20) ); INSERT INTO @Names VALUES ('DeSzmetch'),('DESZMETCH'),('D ...