洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述
Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.
Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.
Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.
John想让他的所有牛用上手机以便相互交流(也是醉了。。。),他需要建立几座信号塔在N块草地中。已知与信号塔相邻的草地能收到信号。给你N-1个草地(A,B)的相邻关系,问:最少需要建多少个信号塔能实现所有草地都有信号。
输入输出格式
输入格式:
Line 1: A single integer: N
Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B
输出格式:
- Line 1: A single integer indicating the minimum number of towers to install
输入输出样例
5
1 3
5 2
4 3
3 5
2
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define man 10050
int n,f[man][4];
//这里的安全表示以x为根的子树全部都安全
//这里的不安全表示以x为根的子树除了x都安全
//f[][0]不放塔但安全
//f[][1]不放塔并且不安全
//f[][2]放塔但安全
//f[][3]放塔还是不安全(肯定不存在,所以不用考虑)
struct edge{int next,to;}e[man<<2];
int head[man<<2],num=0;
inline void add(int from,int to)
{e[++num].next=head[from];e[num].to=to;head[from]=num;}
void treedp(int x,int fa)
{ f[x][0]=f[x][1]=0;f[x][2]=1;int minn=12456789;//当f[][2]时,初始值肯定有他自身的价值
bool changed=0;//记录是否要更改f[x][0]的值
for(int i=head[x];i;i=e[i].next)
{ int to=e[i].to;
if(to==fa)continue;//防止重新搜回去
treedp(to,x);
minn=min(minn,f[to][2]-f[to][0]);//预处理如果将一个点从不放塔变为放塔,那么他的最小代价是多少
f[x][0]+=min(f[to][0],f[to][2]);//先把最小的价值加上去,如果不行的话下面再改;
if(f[to][2]<=f[to][0]) changed=1;//如果放了塔的价值比不放塔的价值还小,那么肯定放塔咯,因为放塔了还可以多照看几个地方(并且还可以把他的父亲一起看了)
f[x][1]+=f[to][0];//本身的f[][1]就是表示不放塔并不安全,那么只能叫它这么继续错下去,因为是当前这个点变得安全是f[][0]和f[][2]的责任,不需要f[][1]的干涉
f[x][2]+=min(f[to][0],min(f[to][1],f[to][2]));//因为当前放了塔并且安全,那么他的子树放不放塔肯定都安全,所以子节点的任何状态都可以成立
}
if(changed==0) f[x][0]+=minn;//如果他的子树都不放塔,那么当前的节点就不安全了,所以必须把他的子节点中放塔的代价最小的点放塔,这样才能保证他的安全
}
int main()
{ scanf("%d",&n);
for(int i=1;i<n;i++)
{ int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
treedp(1,-1);
printf("%d\n",min(f[1][0],f[1][2]));
return 0;
}
洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)的更多相关文章
- 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cel ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...
- luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心
include include include include include include define db double using namespace std; const int N=1e ...
- [USACO08JAN]手机网络Cell Phone Network
[USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phon ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 缩点【洛谷P1262】 间谍网络
[洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines
P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...
- 洛谷 P1262 【间谍网络】
题库 : 洛谷 题号 : 1262 题目 : 间谍网络 link : https://www.luogu.org/problemnew/show/P1262 思路 : 这题可以用缩点的思想来做.先用T ...
随机推荐
- 20155319 2016-2017-2 《Java程序设计》第六周学习总结
20155319 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 ==第十章 输入/输出== 10.1 InputStream与OutputStream 10 ...
- graphql-binding openapi 集成demo
类似的将openapi 转换为graphql api 的也有 https://github.com/yarax/swagger-to-graphql 基本项目 参考代码 https://github. ...
- purescript 基本试用
安装环境 安装预编译文件 https://github.com/purescript/purescript/releases 配置环境变量: export PATH=$PATH:/Users/dalo ...
- Linux配置Nginx+Tomcat负载均衡
cd /usr/local/tomcat1/webapps/ROOT/ tar -zxvf nginx-1.14.2.tar.gz -C /usr/local 一.Linux配置Nginx 一.下载N ...
- maven下载源代码,中文注释乱码的处理方法
通过maven下载的源码,如果有注释是中文的话,一般会有乱码问题,解决办法如下: Window -> Preferences -> Content Types -> Text -&g ...
- Android Studio使用JDBC远程连接mysql的注意事项(附示例)
JDBC为java程序访问各种类型的关系型数据库提供了统一的接口,用户不必针对不同数据库写出不同的代码,但是使用JDBC必须得下载相应的驱动,比如我这里是要连接mysql,于是就到mysql官网去下载 ...
- 租用游艇(简单区间dp)
租用游艇 时间限制: 1 Sec 内存限制: 128 MB提交: 1 解决: 1[提交][状态][讨论版][命题人:quanxing] 题目描述 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2 ...
- Android图片高斯模糊的一些方法
高斯模糊 高斯模糊就是将指定像素变换为其与周边像素加权平均后的值,权重就是高斯分布函数计算出来的值. 一种实现 点击打开链接<-这里是一片关于高斯模糊算法的介绍,我们需要首先根据高斯分布函数计算 ...
- Creating an Android Project(创建一个android项目)
一个android项目包含了你的应用程序中的所有源代码文件,我们可以通过android sdk tools轻松地创建一个拥有默认文件跟文件夹的android项目. 这部分课程我们将展示两种创建andr ...
- httpclient pool帮助类
摘自爬虫类 用于频繁请求减少网络消耗 import java.io.IOException; import java.io.InterruptedIOException; import java.i ...