https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2515

http://7xjob4.com1.z0.glb.clouddn.com/c6a2a6f54f5a6c2cae2c82df2ec552f7

题意:给网络图,叶节点是客户端,其他是服务器,设置最少的服务数在服务器上使客户端到服务的距离不超过指定值

思路:将已有的那个服务作根,转换图至有根树,记录各叶子节点的深度;选深度最大的叶节点的 指定值距离 的父亲节点作为设置服务最划算,设置后dfs覆盖状态,只需处理深度大于指定值的叶子节点。

 #include <bits/stdc++.h>
using namespace std; const int maxn=;
vector <int> gr[maxn],nodes[maxn];
int n,s,k,fa[maxn];
bool covered[maxn]; void dfs(int u,int f,int d)
{
int i,j;
fa[u]=f;
int nc=gr[u].size();
if(nc== && d>k)
{
nodes[d].push_back(u);
}
for(i=;i<nc;i++)
{
int v=gr[u][i];
if(v!=f)
dfs(v,u,d+);
}
return ;
} void dfs2(int u,int f,int d)
{
int i,j;
covered[u]=true;
int nc=gr[u].size();
for(i=;i<nc;i++)
{
int v=gr[u][i];
if(v!=f && d<k)
dfs2(v,u,d+);
}
return ;
} int solve()
{
int ans=;
int i,j;
memset(covered,,sizeof(covered));
for(int d=n-;d>k;d--)
{
for(i=;i<nodes[d].size();i++)
{
int u=nodes[d][i];
if(covered[u]) continue; int v=u;
for(j=;j<k;j++)
v=fa[v];
dfs2(v,-,);
ans++;
}
}
return ans;
} int main()
{
int T;
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&s,&k);
for(i=;i<=n;i++)
{
gr[i].clear();
nodes[i].clear();
}
for(i=;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
gr[u].push_back(v);
gr[v].push_back(u);
}
dfs(s,-,);
printf("%d\n",solve());
}
return ;
}

UVALive 3902 网络的更多相关文章

  1. 2007LA 3902 网络(树+贪心)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=283&am ...

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

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

  3. [UVALive 3902] Network

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

  4. LA 3902 网络

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

  5. UVALive 3902 Network (树+dfs)

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

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

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

  7. 辣些数据结构的思维题(思维题好难一个都不会TAT)

    洛谷P1268 树的重量 我觉得难点在于把每个叶子节点想象成分出来的叉 然后如果c是a--b这条边上分出来的,可以通过Dab,Dca,Dcb算出分叉边的长度, 长度=(Dac+Dbc-Dab)/2 怎 ...

  8. UVALive 5099 Nubulsa Expo 全球最小割 非网络流量 n^3

    主题链接:点击打开链接 意甲冠军: 给定n个点m条无向边 源点S 以下m行给出无向边以及边的容量. 问: 找一个汇点,使得图的最大流最小. 输出最小的流量. 思路: 最大流=最小割. 所以题意就是找全 ...

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

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

随机推荐

  1. Docker容器时间与宿主机时间不一致的问题

    通过date命令查看时间 查看主机时间   1 2 [root@localhost ~]# date 2016年 07月 27日 星期三 22:42:44 CST 查看容器时间   1 2 root@ ...

  2. nginx 配置优化(简单)

    配置文件     正常运行的必备配置:         1.user username [groupname]:(推荐nginx)         以那个用户身份运行,以在configure指定的用户 ...

  3. Union和Union All到底有什么区别

    以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...

  4. SpringMVC常用配置-Controller中的各种配置(基于Java API和注解)

  5. FORTRAN 90标准函数(一) (转)

    符号约定: l  I代表整型;R代表实型;C代表复型;CH代表字符型;S代表字符串;L代表逻辑型;A代表数组;P代表指针;T代表派生类型;AT为任意类型. l  s:P表示s类型为P类型(任意kind ...

  6. linux---------------centos6.4安装完了以后敲ifconfig,没有局域网ip。解决如下

    1.vim /etc/sysconfig/network-scripts/ifcfg-eth0 进入linux然后进入这个文件里面如下: DEVICE=eth0HWADDR=00:0C:29:92:F ...

  7. hduoj 1285 确定比赛名次

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory ...

  8. Access restriction: The type QName is not accessible due to restriction on required library

    There's another solution that also works. I found it on this forum: Go to the Build Path settings in ...

  9. 浅谈大型web系统架构

    动态应用,是相对于网站静态内容而言,是指以c/c++.php.Java.perl..net等服务器端语言开发的网络应用软件,比如论坛.网络相册.交友.BLOG等常见应用.动态应用系统通常与数据库系统. ...

  10. 使用git

    原文出处: 涂根华的博客 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候 ...