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. JAVA字符串比较问题

    在java中值类型通过==来进行比较值是否相等 而字符串作为一种引用类型,通过==是用来比较其内存位置的,使用equals才是用来比较其值是否相等 使用equals时养成将字符串放在前面的好习惯 字符 ...

  2. C# enable为false时不变颜色

            [System.Runtime.InteropServices.DllImport("user32.dll ")]         public static ex ...

  3. 08 SSM整合案例(企业权限管理系统):06.产品操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.用户操作 09.权限控制 10.权限关联与控制 11.AOP日志 06.产品操作 SSM 环境搭 ...

  4. flask 常用数据模型模板

    1.一对多关系模型 示例代码 class Role(db.Model): """角色表""" __tablename__ = 'roles' ...

  5. 重采样Resample 的一些研究记录。

    最近项目有需要重采样算法,先找了一下,主流的就是几个开源算法,Speex / Opus / ffmpeg / sox 1.最早的事Speex,算法源自CCRMA(Center for Computer ...

  6. R 读取excel的方法

    1.加载 readxl 包,利用 reade_excel() 函数 install.packages("readxl") library(readxl) data = read_e ...

  7. 十七、JavaScript之幂运算

    一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...

  8. Block循环引用问题(Objective-c)

    造成循环引用的简单理解是:Block的拥有者在Block作用域内部又引用了自己,因此导致了Block的拥有者永远无法释放内存,就出现了循环引用的内存泄漏 示例代码 @interface ObjTest ...

  9. [转]Spark SQL2.X 在100TB上的Adaptive execution(自适应执行)实践

    Spark SQL是Apache Spark最广泛使用的一个组件,它提供了非常友好的接口来分布式处理结构化数据,在很多应用领域都有成功的生产实践,但是在超大规模集群和数据集上,Spark SQL仍然遇 ...

  10. spring第9天(事务)

    依赖:spring-context,spring-jdbc(这个本身有依赖spring-tx,关于事务的),druid,mysql-connector-java,aspectjweaver五个 由于我 ...