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. 使用git版本控制器C#工程,git托管到GitHub和visual studio on line

    类比TFS, 托管到了VS online,为私有.GitHub上托管的代码为开源. 新建工程选择版本控制器"Git" VS online: 本地: GitHub,下载github ...

  2. IIS 7.5 + PHP-5.6.3 + mysql-5.6.21.1

    禅道项目管理软件源码下载:http://sourceforge.net/projects/zentao/files/6.3/ZenTaoPMS.6.3.stable.zip/download Stp1 ...

  3. ASP.NET corrupt assembly “Could not load file or assembly App_Web_*

    以下是从overFlow 复制过来的问题 I've read through many of the other questions posted on the same issue, but I s ...

  4. 003:Posix IPC的消息队列

    1:与FIFO相比,FIFO要求对一个管道写入之前,必须有进程进行读打开.消息队列则不需要有进行在队列上等待消息的到达. 2:POSIX每次读取总是返回优先级最高的,system V则可以返回任意优先 ...

  5. understanding-论文

    understanding temporal and spatial travel paterns of individual passengers by mining smart card data ...

  6. Zookeeper:通过yarn实现大型分布式管理系统

    http://www.cnblogs.com/leesf456/p/6063694.html

  7. Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  8. PHP小总结

    <?php //1.php基础语法 //输出语句 echo print print_r var_dump() //2.php是弱类型语言 //强制转换类型:(类型)变量 settype(变量,类 ...

  9. Slackware Linux or FreeBSD 配置中文环境。

    配置中文环境. Slackware Linux 如果在控制面板的语言与地区选项中没有找到中文,那说明在安装系统选择软件的时候没有将国际语言支持包选上,可以从slackware的安装盘或ISO文件中提取 ...

  10. there are no packages available for installation插件安装问题和如何配置浏览器的快捷键

    sublime text3 在安装插件时,有时候会莫名其妙的弹出如下所示的弹窗(之前遇到了,但写的时候不知道为什么我的又可以了,这个只是出现了这个问题之后可以尝试的一个解决办法,图片就从网上找了一个) ...