POJ 3659 Cell Phone Network 最小支配集

36 篇文章 0 订阅
32 篇文章 0 订阅
Cell Phone Network
Time Limit: 1000MS      Memory Limit: 65536K
Total Submissions: 6442     Accepted: 2323

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 (1N10,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

Source
USACO 2008 January Gold

2种解法:
①贪心:
如果u没被覆盖,显然 将u的父亲设置成支配集中的点 是最优选择
从叶子节点开始向上进行贪心选择 dfs一遍记录到达的点的顺序 倒过来选择即可
②树形DP:
考虑3个状态:
d[u][0]=u点为支配集中的点 ,以u为根的子树已经被覆盖时,u为根的子树中有几个支配点
d[u][1]=u不为支配集中的点,u为的根的子树中,至少有一个u的孩子是支配点,覆盖了u 此时u为根的子树有几个支配点
d[u][2]=u不为支配集中的点,u为根的子树中,没有一个u的孩子是支配点,u未被覆盖,此时u为根的子树有几个支配点

显然d[u][0]=∑min(d[v][0],d[v][1],d[v][1]) //(v为u的孩子)
d[u][2]:u没被覆盖 不能有d[v][0]状态的孩子,d[v][2]状态的孩子v无法被覆盖,也不能选,所以:
d[u][2]=∑d[v][1] //v为u的孩子
d[u][1]就比较麻烦了 ,显然 d[u][1]状态下 不能有d[v][2]状态的孩子,因为孩子无法被覆盖
当u没有孩子,那u肯定不能被支配 所以d[u][1]=INF
当u有孩子

d[u][1]=∑min(d[v][1],d[v][0])+inc
inc=min(d[v][0]-d[v][1])//选择了一个支配点做儿子

综上:

d[u][0]=∑min(d[v][0],d[v][1],d[v][1])
d[u][2]=∑d[v][1]
d[u][1]=∑min(d[v][1],d[v][0])+inc
inc=min(d[v][0]-d[v][1])

最后 最小支配集=min(d[root][0],d[root][1])

//贪心:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

inline void read(int&x){
    x=0;
    char t;
    while(isdigit(t=getchar()))
        x=x*10+t-'0';
}

const int N=10000+5;
const int M=2*N;
struct Edge{
    int to,next;
}edge[M];

int head[N];

inline void addEdge(int k,int u,int v){
    edge[k]={v,head[u]};
    head[u]=k;
}

int newPos[N];//dfs遍历顺序
int father[N];//父亲节点
int now=0;
void dfs(int u){
    newPos[now++]=u;
    for(int i=head[u];i!=-1;i=edge[i].next){
        if(edge[i].to!=father[u]){
            father[edge[i].to]=u;
            dfs(edge[i].to);
        }
    }
}

bool covered[N];//标记点是否被支配集覆盖
bool isAns[N];//标记点是否属于最小支配集

int greedy(int n){
    int ans=0;
    for(int i=n-1;i>=0;--i){
        int pos=newPos[i];
        if(false==covered[pos]){//如果当前点没被覆盖
            if(false==isAns[father[pos]]){//如果父亲不是支配集的点 设成支配集的点
                isAns[father[pos]]=true;
                ++ans;
            }
            covered[pos]=true;
            covered[father[pos]]=true;
            covered[father[father[pos]]]=true;
        }
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n,u,v;
    scanf("%d",&n);
    fill(head,head+n+1,-1);
    for(int i=0;i<n-1;++i){
        scanf("%d%d",&u,&v);
        addEdge(2*i,u,v);
        addEdge(2*i+1,v,u);
    }
    father[1]=1;
    dfs(1);
    printf("%d\n",greedy(n));
    return 0;
}
//树形DP
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

inline void read(int&x){
    x=0;
    char t;
    while(isdigit(t=getchar()))
        x=x*10+t-'0';
}

const int N=10000+5;
const int M=2*N;
struct Edge{
    int to,next;
}edge[M];

int head[N];

inline void addEdge(int k,int u,int v){
    edge[k]={v,head[u]};
    head[u]=k;
}


int d[N][3];

void DP(int u,int father){
    d[u][0]=1;
    d[u][1]=INF;
    d[u][2]=0;
    bool flag=false;
    int sum=0;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int to=edge[i].to;
        if(to==father)
            continue;
        DP(to,u);
        d[u][0]+=min(d[to][0],min(d[to][1],d[to][2]));
        d[u][2]+=d[to][1];
        d[u][2]=min(d[u][2],INF);
        if(d[to][0]<=d[to][1]){
            flag=true;
            sum+=d[to][0];
        }
        else
            sum+=d[to][1];
    }

    if(flag==false){
        for(int i=head[u];i!=-1;i=edge[i].next){
            int to=edge[i].to;
            if(to!=father)
                d[u][1]=min(d[u][1],sum-d[to][1]+d[to][0]);
        }
    }
    else
        d[u][1]=sum;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n,u,v;
    scanf("%d",&n);
    fill(head,head+n+1,-1);
    for(int i=0;i<n-1;++i){
        scanf("%d%d",&u,&v);
        addEdge(2*i,u,v);
        addEdge(2*i+1,v,u);
    }
    DP(1,1);
    printf("%d\n",min(d[1][1],d[1][0]));
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值