POJ 3659 Cell Phone Network【最小支配集 dp && 贪心】

Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6231 Accepted: 2230

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

Source


恩,在一个地方建塔,可以向与其相邻的地区提供服务,问最少建多少个塔。

动态规划三个状态:

dp[u][0]:当前点建塔,其所有子树都被覆盖  

dp[u][1]:当前点不建塔,其所有子树都被覆盖,而当前点被其孩子覆盖    

dp[u][2]:当前点不建塔,其所有子树都被覆盖,而当前点不被孩子覆盖,即被当前点的父节点覆盖,也就是说当前点的孩子都没建塔。(所以此状态的转移方程有点不懂)

如果有理解的,希望能麻烦给解释一下,谢谢。


#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 100010
#define inf 0x3f3f3f3f
using namespace std;
int cnt,head[maxn];
int dp[maxn][3];
struct node
{
    int f,t,next;
};
node edge[maxn];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(dp,0,sizeof(dp));
}
void add(int f,int t)
{
    edge[cnt].f=f;
    edge[cnt].t=t;
    edge[cnt].next=head[f];
    head[f]=cnt++;
}
void DP(int u,int p)
{
    bool sign;
    int sum,inc;
    dp[u][2]=0;
    dp[u][0]=1;
    sum=0;
    inc=inf;
    sign=false;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(v==p)
            continue;
        DP(v,u);
        dp[u][0]+=min(dp[v][0],min(dp[v][1],dp[v][2]));
        dp[u][2]+=min(dp[v][0],dp[v][1]);//这里个人认为是与孩子节点的第一种状态无关的,但只有这样才A
        if(dp[v][0]<=dp[v][1])
        {
            sum+=dp[v][0];
            sign=true;
        }
        else
        {
            sum+=dp[v][1];
            inc=min(inc,dp[v][0]-dp[v][1]);
        }
//        if(dp[v][2]!=inf&&!sign)
//            dp[u][2]+=dp[v][1];//这里个人认为应该是这样的,因为此状态表示其孩子都没建塔而又被覆盖,所以只与孩子的第二种状态有关,但这样WA,不懂不懂
//        else
//            dp[u][2]=inf;
    }
    if(inc==inf&&!sign)
        dp[u][1]=inf;
    else
    {
        dp[u][1]=sum;
        if(!sign)
            dp[u][1]+=inc;
    }
}
int main()
{
    int n,a,b;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<n;++i)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        DP(1,n);
        int ans=min(dp[1][0],dp[1][1]);
        printf("%d\n",ans);
    }
    return 0;
}

 

贪心,这里首先dfs一遍,并记录顺序,之后反序,才能保证当其子树都被处理之后才会处理该节点,若当前点不被覆盖,而且它的父节点也不属于支配集,支配集中个数加1 ,标记当前点,当前点的父节点和当前点的父节点的父节点为覆盖,因为这些点要么属于支配集,要么与支配集中的点相连,即被覆盖。

 

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 100010
#define inf 0x3f3f3f3f
using namespace std;
int cnt,head[maxn];
int p[maxn],newpos[maxn];
int now,n;
bool select[maxn];
struct node
{
    int f,t,next;
};
node edge[maxn];
void init()
{
    cnt=now=0;
    memset(head,-1,sizeof(head));
    memset(select,false,sizeof(select));
    select[1]=true;
    memset(newpos,0,sizeof(newpos));
    memset(p,0,sizeof(p));
}
void add(int f,int t)
{
    edge[cnt].f=f;
    edge[cnt].t=t;
    edge[cnt].next=head[f];
    head[f]=cnt++;
}
void dfs(int x)
{
    newpos[now++]=x;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        int v=edge[i].t;
        if(!select[v])
        {
            select[v]=true;
            p[v]=x;
            dfs(v);
        }
    }
}
int greedy()
{
    bool s[maxn],ss[maxn];
    int ans=0;
    memset(s,false,sizeof(s));
    memset(ss,false,sizeof(ss));
    for(int i=n-1;i>=0;--i)
    {
        int t=newpos[i];
        if(!s[t])
        {
            if(!ss[p[t]])
            {
                ss[p[t]]=true;
                ans++;
            }
            s[t]=true;
            s[p[t]]=true;
            s[p[p[t]]]=true;
        }
    }
    return ans;
}
int main()
{
    int a,b;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<n;++i)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        dfs(1);
        int ans=greedy();
        printf("%d\n",ans);
    }
    return 0;
}


 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值