【POJ 3659】Cell Phone Network

Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6012 Accepted: 2163

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

 
真是蛋疼啊。理解错题意了。以为是覆盖边了。后来看了神犇的BLOG才恍然大悟。。。
还是DP;
以任意节点为根开始DFS+DP(就是树形DP嘛)
3种情况,,
  (1)编号为x的节点建塔,保证x和其子孙被基塔覆盖。
  (2)编号为x的节点不建塔,保证x和其子孙被基塔覆盖。
  (3)编号为x的节点不建塔,保证x没有被覆盖,但x的子孙已被覆盖。
就这么多,不难写出转移方程了。
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int MAXN = 10005;

struct Edge
{
    int from, to, next;
    
    Edge() {
    }
    
    Edge(int u, int v) : from(u), to(v) {
    }
    
}edges[MAXN << 1];

int first[MAXN], tots;
int f[MAXN][3], n;

void add(int a, int b)
{
    edges[tots] = Edge(a, b);
    edges[tots].next = first[a];
    first[a] = tots++;
}

void dfs(int x)
{
    memset(f[x], 0, sizeof(f[x]));
    bool flag = true;
    int mint = 0x7fffffff;
    for (int i = first[x]; i != -1; i = edges[i].next)
    {
        Edge &e = edges[i];
        if (f[e.to][0] == -1)
        {
            dfs(e.to);
            f[x][0] += min(f[e.to][0], min(f[e.to][1], f[e.to][2]));
            f[x][2] += min(f[e.to][0], f[e.to][1]);
            if (f[e.to][0] <= f[e.to][1])
            {
                flag = false;
                f[x][1] += f[e.to][0];
            }
            else
            {
                mint = min(mint, f[e.to][0] - f[e.to][1]);
                f[x][1] += f[e.to][1];
            }
        }
    }
    if (flag) f[x][1] += mint;
    f[x][0]++;
}

int main()
{
    scanf("%d", &n);
    tots = 0;
    memset(first, -1, sizeof(first));
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        add(u, v);
        add(v, u);
    }
    memset(f, -1, sizeof(f));
    dfs(1);
    printf("%d\n", min(f[1][0], f[1][1]));
    return 0;
}

 

转载于:https://www.cnblogs.com/albert7xie/p/4804173.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值