Uva 3902 Network
题目大意:
在非叶子节点上安装最少的服务器使得,每个叶子节点到服务器的距离不超过k。
贪心+图上的dfs。 先从深度最大的叶子节点开始找。找到父节点后再用这个父节点进行扩充。
/* ***********************************************
Author :guanjun
Created Time :2016/5/10 23:15:38
File Name :7.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
int n,s,k;
vector<int>gr[maxn];
vector<int>nod[maxn];
int fa[maxn];
int vis[maxn];
void dfs(int f,int u,int d){
fa[u]=f;
int x=gr[u].size();
if(x==&&d>k)nod[d].push_back(u);
for(int i=;i<x;i++){
int v=gr[u][i];
if(v!=f)dfs(u,v,d+);
}
}
void dfs1(int f,int u,int d){
vis[u]=;
int x=gr[u].size();
for(int i=;i<x;i++){
int v=gr[u][i];
if(v!=f&&d<k)dfs1(u,v,d+);
}
}
int solve(){
int ans=;
cle(vis);
for(int d=n-;d>k;d--){
for(int j=;j<nod[d].size();j++){
int u=nod[d][j];
if(vis[u])continue;
int v=u;
for(int i=;i<k;i++)v=fa[v];
ans++;
dfs1(-,v,);
}
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,a,b;
cin>>t;
while(t--){
cin>>n>>s>>k;
cle(fa);
for(int i=;i<=n;i++)gr[i].clear(),nod[i].clear();
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
gr[a].push_back(b);
gr[b].push_back(a);
}
dfs(-,s,);
printf("%d\n",solve());
}
return ;
}
Uva 3902 Network的更多相关文章
- 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 ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- LA 3902 Network(树上最优化 贪心)
Network Consider a tree network with n <tex2html_verbatim_mark>nodes where the internal nodes ...
- UVALive 3902 Network (树+dfs)
Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal ...
- LA 3902 Network
人生第一道图论题啊,有木有 题意: 有一个树状网络,有一个原始服务器s,它的服务范围是k 问至少再放多少台服务范围是k的服务器才能使网络中的每个节点都被覆盖掉 解法: 我们以原始服务器为根将其转化成一 ...
- Uva 315 Network 判断割点
模板题,注意输出 #include <stdio.h> #include <string.h> #include <algorithm> #include < ...
- uva 1267 - Network
这个题目关键在于把无根树变成有根树:这个用dfs: 然后用贪心的方法,从最深的那层开始,每次找到节点的上k层,建一个服务器,然后用一个dfs把这个服务器能够覆盖的节点标记: #include<c ...
- 无向图求割点 UVA 315 Network
输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #in ...
随机推荐
- mysql解压之后的安装
远程连接报错(error:10061)看这篇:https://www.cnblogs.com/zipon/p/5877820.html 从5.6.20之后root会自动生成一个随机密码在/root/. ...
- 淘金(bzoj 3131)
Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块. 一阵风吹 ...
- 【SPOJ687&POJ3693】Maximum repetition substring(后缀数组)
题意: n<=1e5 思路: From http://hzwer.com/6152.html 往后匹配多远 r 用ST表求lcp即可...往前 l 就把串反过来再做一下.. 但是有可能求出来的最 ...
- golang测试框架--smartystreets/goconvey
视频教程和配套博客:goconvey - 课时 1:优雅的单元测试 Go 语言虽然自带单元测试功能,在 GoConvey 诞生之前也出现了许多第三方辅助库.但没有一个辅助库能够像 GoConvey 这 ...
- 漫话最小割 part1
codeforces 724D [n个城市每个城市有一个特产的产出,一个特产的最大需求.当i<j时,城市i可以运最多C个特产到j.求所有城市可以满足最大的需求和] [如果直接最大流建图显然会T. ...
- Codeforces 757 F Team Rocket Rises Again
Discription It's the turn of the year, so Bash wants to send presents to his friends. There are n ci ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享
说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...
- Maven插件开发教程收集(待实践)
官方教程:http://maven.apache.org/plugin-developers/index.html http://blog.csdn.net/csfreebird/article/de ...
- Answer's Question about pointer
When you create a new pointer, this will be in heap until you delete it. So what you said is sort o ...
- VC++_错误 无法打开包括文件“glglut.h” No such file or directory 怎么办
在网上看到类似的问题,查找资料找到了解决方案,现整理如下,有些更改,好让自己多些印象,附原文网址:http://blog.csdn.net/bigloomy/article/details/62265 ...