Cell Phone Network
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5325   Accepted: 1886

Description

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 ≤ NA ≠ B) there is a sequence of adjacent pastures such that 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.

Input

* 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

Output

* Line 1: A single integer indicating the minimum number of towers to install

Sample Input

5
1 3
5 2
4 3
3 5

Sample Output

2

这道题卡了我挺久的,主要是还是沿用覆盖边的那个旧思想,通过这一题,明白了做树形DP最重要就是找准状态有哪几种。像之前那个覆盖边的题目,状态就两种,顶多算三种吧,己有子无的最优,己有子有的最优,己无子有的最优,。。。不过前两种完全可以合并成一个,通过min函数挑选出最优
但是这个题目,相对于前三种,就多了一个状态,己无子无,这个是可以存在的,因为只需要覆盖点,所以在搜索过程中,完全可以出现这种情况
所以为了照顾最后一种,只能增加一个dp[i][0] 即为i未放点,且i未被覆盖(说明子未放点)的最优值。。。当然这个肯定不是最优的,但是这个量为父亲挑选起了很大作用
故dp[i][0]+=dp[son][2];
(己有的最优值)dp[i][1]+=dp[son][0],dp[son][1],dp[son][2]的最小
(己无得最优)dp[i][2]+=dp[i][1],dp[i][2]的最小(此式不完全正确,下面分析)
请特别注意,如果dp[i][2]选的全部都是儿子的dp[i][2],意味着不满足条件,一定至少要儿子有一个点为dp[son][1];
我当时智商拙计,想到了这里,还是没想明白怎么判断是否取了不满足的条件
后来原来是统计下所有dp[son][1]>dp[son][2]用一个tmin记录下dp[son][1]-dp[son][2]的最小值,并且用一个bool变量判断是否有dp【son】【1】(只需要儿子中的一个dp[son][1]<=dp[son][2]即可)。。。如果没有dp【son】【1】,则dp[i][2]+=tmin;即可求得最优 还有,处理最底下的叶子的时候,dp[叶子][2]按定义来说好像是0,但是这个又不能成立,可以虚拟一个虚点连接叶子用来覆盖,但不计数,使得dp[叶子][2]=1;
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define N 10005
#define INF 99999999
using namespace std;
vector<int> v[N];
int dp[N][];
bool vis[N];
void dfs(int root)
{
vis[root]=;
dp[root][]=dp[root][]=;
dp[root][]=;
int temp=,tmin=INF;
bool flag=false,ff=false;
for (int i=;i<v[root].size();i++)
{
int x=v[root][i];
if (vis[x]) continue;
dfs(x);
dp[root][]+=min(dp[x][],min(dp[x][],dp[x][]));
dp[root][]+=dp[x][];
dp[root][]+=min(dp[x][],dp[x][]);
if (dp[x][]>dp[x][]) tmin=min(tmin,dp[x][]-dp[x][]);
else flag=true;
ff=true;
}
if (!flag && ff) dp[root][]+=tmin;
if (!ff) dp[root][]=;//叶子情况,但是此时脑子里虚拟出一个不存在的点来覆盖叶子点,使得dp[root][2]能顺理成章的=1;
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int a,b;
int i,j;
for (i=;i<n;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
memset(vis,,sizeof vis);
dfs();
printf("%d\n",min(dp[][],dp[][]));
}
return ;
}

POJ 3659 再谈树形DP的更多相关文章

  1. 再谈树形dp

    上次说了说树形dp的入门 那么这次该来一点有难度的题目了: UVA10859 Placing Lampposts 给定一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都与灯相邻(被灯照亮 ...

  2. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  3. POJ - 3162 Walking Race 树形dp 单调队列

    POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...

  4. POJ 2486 Apple Tree(树形DP)

    题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...

  5. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  6. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

  7. 再探树形dp

    随着校oj终于刷进了第一页,可以不用去写那些水题了,开始认真学习自己的东西,当然包括文化课.努力.. 这道题呢是道树形dp,可看到了根本就不知道怎么写思考过程: 5min 终于看懂了题 画了样例的图把 ...

  8. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  9. POJ 2152 Fire (树形DP,经典)

    题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站 ...

随机推荐

  1. 将xml字符串的所有叶标签转换成Map集合

    实际问题:对方服务器接口采用webservice方式返回xml报文,现需解析xml获取所有叶节点的标签名及携带的值 解决方案:利用dom4j解析xml并利用递归获取叶节点,将标签名及标签值封装到Map ...

  2. 抓包工具fiddler的Https证书设置

    一.工具(option)--设置(setting)-- https-- 动作(actions)-- (open windows certificate manger)-- 搜索(fiddler)删除所 ...

  3. Python基础week1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  4. C++ MySQL封装类

    #ifndef MYSQL_MANAGER_H #define MYSQL_MANAGER_H #include <Winsock2.h> #include "mysql.h&q ...

  5. Python的一些常用知识

    1.How to force urllib2 not to use a proxy Here is an example to remove proxy settings for all reques ...

  6. 用CSS编写多种常见的图形

    用CSS编写多种常见的图形 正方形与长方形 这个是最简单的,直接上代码 <!DOCTYPE html> <html> <head> <title>< ...

  7. 087-把PHP数组中的元素按随机顺序重新排列shuffle

    <?php $arr=array(3,23,'A','f','123','hello'); //定义一个数组 echo '排序之前的数组信息:<br>'; print_r($arr) ...

  8. crontab 实现Linux系统上定时任务的关键命令

    附上原文链接 https://www.cnblogs.com/ftl1012/p/crontab.html 原文真是写得非常好,所谓他山之石可以攻玉~,我就不客气啦,哈哈

  9. C#使窗体不显示在任务栏

    this.ShowInTaskbar = false;///使窗体不显示在任务栏

  10. 浅谈Python之sys.argv

    (1)sys.argv是什么 sys模块为进入解释器维护或使用的变量,以及与解释器相关的函数提供了途径.sys.argv在脚本程序中扮演了这样一个角色:将命令行输入的参数作为一个list传入脚本程序, ...