poj 3659 Cell Phone Network(树dp+树的最小支配集)

14 篇文章 0 订阅
Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5207 Accepted: 1835

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 ≤ AN; 1 ≤ BN; AB) 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
 
题意:有一个图G,现在希望在一些点建立控制站,每个控制站能控制与它直接相邻的点,现在希望有选择的在一些点建立控制站,使得以最小得控制站数,控制所有的点
思路:求树的最小支配集。
      设dp[u][0]表示选择u这个点,且以u为根的子树完全被覆盖的最小个数。
      设dp[u][1]表示u这个点被其儿子覆盖,且以u为根的子树完全被覆盖的最小个数。
      设dp[u][2]表示u这个点被其父亲覆盖,且以u为根的子树完全被覆盖的最小个数。
 
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <map>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=10005;
const int INF=1000000000;
vector<int>G[maxn];
int dp[maxn][3];
bool vis[maxn];
int n;
void input()
{
    int a,b;
    scanf("%d",&n);
    for(int i=0;i<n-1;i++)
    {
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
}
void dfs(int u,int fa)
{
    dp[u][0]=1;
    dp[u][1]=INF;
    dp[u][2]=0;
    bool isleaf=true;
    for(int i=0;i<(int)G[u].size();i++)
    {
        int v=G[u][i];
        if(v==fa) continue;
        dfs(v,u);
        isleaf=false;
        dp[u][0]+=min(dp[v][0],min(dp[v][1],dp[v][2]));
        dp[u][2]+=min(dp[v][0],dp[v][1]);
    }
    if(!isleaf)
    {
        for(int i=0;i<(int)G[u].size();i++)
        {
            int v=G[u][i];
            if(v==fa) continue;
            dp[u][1]=min(dp[u][1],dp[u][2]-min(dp[v][0],dp[v][1])+dp[v][0]);
        }
    }
}
void solve()
{
    dfs(1,-1);
    printf("%d\n",min(dp[1][0],dp[1][1]));
}
int main()
{
    input();
    solve();
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值