SGU 280.Trade centers(贪心)
SGU 280.Trade centers 解题报告
题意:
n(<=30000)个城市,(n-1)条道路,求最少需要选择多少个城市建造市场,使得所有城市到任意一个市场的距离不大于k。
Solution:
比较好的贪心题。实现起来也有一定技巧。
先以任意点为根,构造出一颗有根树。
首先比较容易想到的是从叶子节点向上寻找,如果只有一个距离为k的点,就把它选上。但是有多个呢?
于是思考更一般的做法,由于是树形结构,先考虑以x节点为根的子树。我们先假设f[x]代表离x节点向下的市场点的距离,这个值可以通过子树的f[]值得出。但是子树有多个f[]我们需要的是哪个呢?
先来考虑x的儿子p,假设f[p]<=k,那么p已经被覆盖,并可能覆盖到x和它的父节点和它的兄弟子树。假设f[p]>k,那么p需要在x或它的父亲中建造市场,要么其实他已经被它的兄弟子树覆盖。这个可以通过判断$max(f[p])+min(f[p])+2$是否小于等于2*k+1来判断。
如果x的子树全部被覆盖了,那么$f[x] = min(f[p])+1$,
如果没有,$f[x]=max(f[p])+1$,并且如果f[x]=2*k+1那么让x成为关键点。即令$f[x]=0$。
注意在根的时候,根节点无法向上,如果f[root]>k说明子树中有节点需要在根节点建造市场。
根据定义叶子节点k应该满足f[k]=k+1。
最后输出f[]为0的节点就行了。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int INF = 31111;
vector<int> edge[INF], ans;
int f[INF];
int n, k, tol;
int dfs ( int x , int fa )
{
int dmax = -INF, dmin = INF, s = 0;
f[x] = k + 1;
for ( int i = 0; i < edge[x].size(); ++i ) {
int v = edge[x][i];
if ( v == fa ) continue;
++s;
dfs ( v , x );
dmin = min ( f[v], dmin );
dmax = max ( f[v], dmax );
}
if ( s ) {
if ( dmin + dmax + 2 <= 2 * k + 1 ) f[x] = dmin + 1;
else f[x] = dmax + 1;
}
if ( f[x] == 2 * k + 1 ) {
f[x] = 0;
++tol;
}
}
int main()
{
scanf ( "%d %d", &n, &k );
for ( int i = 2, x, y; i <= n; ++i ) {
scanf ( "%d %d", &x, &y );
edge[x].push_back ( y );
edge[y].push_back ( x );
}
dfs ( 1 , 0 );
if ( f[1] > k ) ++tol, f[1] = 0;
printf ( "%d\n", tol );
for ( int i = 1; i <= n; i++ )
if ( f[i] == 0 ) printf ( "%d\n", i );
}
/*
14 3
1 2
1 3
1 4
2 5
2 6
3 7
3 8
4 9
7 10
7 11
10 12
11 13
13 14
answer:
2
1
7
*/
SGU 280.Trade centers(贪心)的更多相关文章
- HDU1009_FatMouse' Trade【贪心】【水题】
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [题解]hdu 1009 FatMouse' Trade(贪心基础题)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- hdu 1009:FatMouse' Trade(贪心)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1009 FatMouse' Trade (贪心算法)
题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...
- HDU1009:FatMouse' Trade(初探贪心,wait)
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...
- HDU——1009FatMouse' Trade(贪心+结构体+排序)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 杭电 1009 FatMouse' Trade (贪心)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- HDU 1009 FatMouse' Trade【贪心】
解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum 即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆 ...
- HDU 1009 FatMouse' Trade (贪心)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009 题目大意:肥鼠准备了 磅的猫粮,准备和看管仓库的猫交易,仓库里装有他最喜爱的食物 豆.仓库有 个 ...
随机推荐
- linux select 与 阻塞( blocking ) 及非阻塞 (non blocking)实现io多路复用的示例
除了自己实现之外,还有个c语言写的基于事件的开源网络库:libevent http://www.cnblogs.com/Anker/p/3265058.html 最简单的select示例: #incl ...
- java 日志技术汇总(log4j , Commons-logging,.....)
前言 在Tomcat 与weblogic 中的 日志(log4j) 配置系列一 在系列一 中, 有一个问题一直没有解决,就是部署到weblogic 中应用程序如何通过log4j写日志到文件中? 这里仅 ...
- js 复制网页内容,兼容各浏览器
因需要做一个js单击,复制当前网页url的功能.使用的是如下的方法,但是只能在ie浏览器下正常使用. 方法如下: function copyURL(){ var clipBoardContent=&q ...
- Android---用Wi-Fi来建立对等连接
本文译自:http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html WiFi对等API(P2P ...
- setvbuf
函数名: setvbuf 用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size); type : 期望缓冲区的类型: _I ...
- ecto注册码
First name: Good Last name: Life Serial: ecto_at585-RP00-MP3F-VB8R-L82N-N0CC First Name: The Last ...
- ArrayList的实现原理--转
1. ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部 ...
- Enable SPI 1.0 and 1.1 with device tre overlays on BeagleBone
For most people the above image means absolutely nothing, but for that one guy that has been searchi ...
- 如何在 Objective-C 的环境下实现 defer
关注仓库,及时获得更新:https://github.com/draveness/iOS-Source-Code-Analyze Follow: https://github.com/Dravenes ...
- Socket异步通信学习一
最近在做一个频谱管理项目,负责通信模块,自己也是小白,重头学起,直至今天通信基本框架已经完成,把自己在学习中的心得与大家分享一下,做一个socket系列的博文,顺便加固一下自己对socket通信的认识 ...