POJ 3659 再谈树形DP

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][4];
bool vis[N];
void dfs(int root)
{
    vis[root]=1;
    dp[root][0]=dp[root][2]=0;
    dp[root][1]=1;
    int temp=0,tmin=INF;
    bool flag=false,ff=false;
    for (int i=0;i<v[root].size();i++)
    {
        int x=v[root][i];
        if (vis[x]) continue;
        dfs(x);
        dp[root][1]+=min(dp[x][0],min(dp[x][1],dp[x][2]));
        dp[root][0]+=dp[x][2];
        dp[root][2]+=min(dp[x][1],dp[x][2]);
        if (dp[x][1]>dp[x][2]) tmin=min(tmin,dp[x][1]-dp[x][2]);
        else flag=true;
        ff=true;
    }
    if (!flag && ff) dp[root][2]+=tmin;
    if (!ff) dp[root][2]=1;//叶子情况,但是此时脑子里虚拟出一个不存在的点来覆盖叶子点,使得dp[root][2]能顺理成章的=1;
}
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        int a,b;
        int i,j;
        for (i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            v[b].push_back(a);
        }
        memset(vis,0,sizeof vis);
        dfs(1);
        printf("%d
",min(dp[1][1],dp[1][2]));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kkrisen/p/3383609.html