题目描述

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

解析:当然是树形dp了;本题很好的参照是一道叫做   皇宫看守  的树形dp(在小白菜oj上可以看到,还有题解和视频解析)

解释全在代码里:

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define man 10050
int n,f[man][];
//这里的安全表示以x为根的子树全部都安全
//这里的不安全表示以x为根的子树除了x都安全
//f[][0]不放塔但安全
//f[][1]不放塔并且不安全
//f[][2]放塔但安全
//f[][3]放塔还是不安全(肯定不存在,所以不用考虑)
struct edge{int next,to;}e[man<<];
int head[man<<],num=;
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][]=f[x][]=;f[x][]=;int minn=;//当f[][2]时,初始值肯定有他自身的价值
bool changed=;//记录是否要更改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][]-f[to][]);//预处理如果将一个点从不放塔变为放塔,那么他的最小代价是多少
f[x][]+=min(f[to][],f[to][]);//先把最小的价值加上去,如果不行的话下面再改;
if(f[to][]<=f[to][]) changed=;//如果放了塔的价值比不放塔的价值还小,那么肯定放塔咯,因为放塔了还可以多照看几个地方(并且还可以把他的父亲一起看了)
f[x][]+=f[to][];//本身的f[][1]就是表示不放塔并不安全,那么只能叫它这么继续错下去,因为是当前这个点变得安全是f[][0]和f[][2]的责任,不需要f[][1]的干涉
f[x][]+=min(f[to][],min(f[to][],f[to][]));//因为当前放了塔并且安全,那么他的子树放不放塔肯定都安全,所以子节点的任何状态都可以成立
}
if(changed==) f[x][]+=minn;//如果他的子树都不放塔,那么当前的节点就不安全了,所以必须把他的子节点中放塔的代价最小的点放塔,这样才能保证他的安全
}
int main()
{ scanf("%d",&n);
for(int i=;i<n;i++)
{ int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
treedp(,-);
printf("%d\n",min(f[][],f[][]));
return ;
}

洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network的更多相关文章

  1. 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network

    P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cel ...

  2. 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)

    题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...

  3. P2899 [USACO08JAN]手机网络Cell Phone Network

    P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...

  4. luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心

    include include include include include include define db double using namespace std; const int N=1e ...

  5. [USACO08JAN]手机网络Cell Phone Network

    [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phon ...

  6. 缩点【洛谷P1262】 间谍网络

    [洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...

  7. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines

    P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...

  8. 洛谷 P1262 【间谍网络】

    题库 : 洛谷 题号 : 1262 题目 : 间谍网络 link : https://www.luogu.org/problemnew/show/P1262 思路 : 这题可以用缩点的思想来做.先用T ...

  9. Arctic Network(洛谷)--北极通讯网络(loj)

    洛谷传送门 loj传送门 一道蛮基础的最小生成树的题 题意也没绕什么圈子 只是叙述的有点累赘而已(loj上是这样的 也就读入加建边需要稍稍稍多想一下下 对于我这么一个蒟蒻 这是一道很好的板子题 (洛谷 ...

随机推荐

  1. Unit08: Spring集成mybatis

    Unit08: Spring集成mybatis 1. Spring集成mybatis (1)方式一 step1. 导包. spring-webmvc,mybatis,mybatis-spring, o ...

  2. 贴几个erlang文档站点

    国外三方的文档,比较全, http://erldocs.com/ 这个貌似是国内的版本,不是很新 http://erldoc.com/ 国内dhq大神的,也不是很新 http://dhq.me/erl ...

  3. C# 中的委托和事件(1)

    引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...

  4. 操作系统-服务器-百科:Windows Server

    ylbtech-操作系统-服务器-百科:Windows Server Windows Server是微软在2003年4月24日推出的Windows 的服务器操作系统,其核心是Microsoft Win ...

  5. 【BZOJ】1823: [JSOI2010]满汉全席(2-sat)

    题目 传送门:QWQ 分析 2-sat模板(然而辣鸡如我还是调了好久) 代码 //bzoj 1823 2-sat #include <bits/stdc++.h> using namesp ...

  6. 侯捷 c++面向对象程序设计

    基础知识 基于对象:Object Based 面对的是单一class的设计. 面向对象:Object Oriented 面对的是多重classes的设计,涉及到类和类之间的关系. 课程中设计到两种不同 ...

  7. docker中部署mongodb副本集

    1.基本信息如下 服务器地址 192.168.73.129 副本集名称 rs 容器节点及端口映射         m0 37017:27017         m1 47017:27017       ...

  8. 自己动手实现XXX系列

    前记: 最近看了rongjun的一片文章:自己动手实现jdk代理类.按照上面的例子敲完才发现,JDK动态代理 实现底层原来如此简单,只是大量的使用了反射,类编译,类加载一些常规的东西 而且本质也是如实 ...

  9. DirectShow的RTP发包(H264)Filter <转>

    转帖地址:http://blog.csdn.net/fan2273/article/details/77653700 DirectShow的RTP发包(H264)Filter 基于DirectShow ...

  10. Delphi三层开发小技巧:TClientDataSet的Delta妙用

    Delphi三层开发小技巧:TClientDataSet的Delta妙用 转载 2014年10月13日 09:41:14 标签: 三层 / ClientDataSet 318 from :http:/ ...