H - Cell Phone Network(最小支配集:树形dp)

题目链接
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.

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[i][state]:以i为根节点,并且其状态为state的情况下,其子树中被染色的点的个数的最小值

dp[i][0]:i不染色,父节点染色覆盖i

dp[i][1]:i不染色,子节点染色覆盖i

dp[i][2]:i染色

对于状态0:该点已被其父节点覆盖,所以它的子节点可以是染色的,也可以是不染色的。

因此:设v为i的子节点,dp[i][0] += min(dp[v][1], dp[v][2]);

对于状态1:该节点需要被其子节点覆盖,因此其子节点中至少要有一个是染色的。

因此:设v为i的子节点,dp[i][1] += min(dp[v][1], dp[v][2]),同时保存Min = min(Min, dp[v][2] - dp[v][1])。

若所有子节点dp[v][1]<dp[v][2](即所有子节点都不染色),那么dp[root][1] += Min(即强迫一个子节点染色且保证最优解)。

对于状态2:因为该节点本身染色,因此其子节点可以依赖它,也可以依赖它的子节点,也可以其本身就染色。

因此:设v为i的子节点,dp[i][2] += min(min(dp[v][0], dp[v][1]), dp[v][2]);

此外,初始化时dp[i][2]=1,因为其本身染色。

另外就是叶子节点无状态1,根节点无状态0。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn=10010;
const int inf=1e9+7;
vector<int> tree[maxn];
int n;
int dp[maxn][3];
int vis[maxn];//标记是否被访问
/*
dp[i][0]:i不染色,父节点染色覆盖i
dp[i][1]:i不染色,子节点染色覆盖i
dp[i][2]:i染色
dp[i][state]:以i为根节点,并且其状态为state的情况下,其子树中被染色的点的个数的最小值
*/

void dfs(int u,int v)
{
    //叶节点
    if(u!=1&&tree[u].size()==1)
    {
        dp[u][0]=0;
        dp[u][1]=inf;//叶节点无子节点
        dp[u][2]=1;
        return ;
    }
    int fg=0,Min=inf;
    dp[u][2]=1;
    for(int i=0;i<tree[u].size();i++)
    {
        int x=tree[u][i];
        if(x==v)
            continue;
        dfs(x,u);
        dp[u][0]+=min(dp[x][1],dp[x][2]);
        dp[u][2]+=min(min(dp[x][0],dp[x][1]),dp[x][2]);
        if(dp[x][2]>dp[x][1]){
            dp[u][1]+=dp[x][1];
            Min=min(Min,dp[x][2]-dp[x][1]);
        }
        else
        {
            fg=1;
            dp[u][1]+=dp[x][2];
        }
    }
    if(!fg)
        dp[u][1]+=Min;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        tree[u].push_back(v);
        tree[v].push_back(u);
    }
    dfs(1,-1);
    int ans=min(dp[1][1],dp[1][2]);
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值