POJ 3659 Cell Phone Network (树dp)

来源:互联网 发布:cdma网络是什么意思 编辑:程序博客网 时间:2024/05/21 11:02

大体题意:

给你一个棵树,要求选择尽量少的点,覆盖所有的点,选择一个点后,它相邻的点全都覆盖。

思路:

很明显树形dp。

令dp[i][0],表示  选择i 这个点, 覆盖了以i 为根的子树所有的点。

dp[i][1]表示 i 这个点不选,但选了 它的孩子一个结点,覆盖了以i 为根的子树所有的点。

dp[i][2]表示i 这个点不选,但选了它的父亲,覆盖了 以i 为根的子树的所有结点。

转移方程:

dp[i][0]很好说,既然选了i 这个点,那么它的孩子 选或不选 就随便了   dp[cur][0] += Min(dp[v][0],Min(dp[v][1],dp[v][2]));

dp[i][2]  也很好弄,i 不选,但选了它的父亲,那么i 已经被覆盖了, 不用管, 它的孩子选或不选都行了,dp[cur][2] += Min(dp[v][0],dp[v][1]);

dp[i][1]  i 不选,选了它的孩子,  和dp[i][2]一样,  但是有一种情况 ,就是 全都选了dp[j][1],这样i 这个点就不被覆盖了。

这种情况单独考虑,必须找一个孩子选了。

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define Siz(x) (int)x.size()using namespace std;const int maxn = 10000 + 10;const int inf = maxn*10;vector<int>g[maxn];int n;int dp[maxn][3];void dfs(int cur,int f){    if (Siz(g[cur]) == 1 && g[cur][0] == f){        dp[cur][0] = 1;        dp[cur][1] = inf;        dp[cur][2] = 0;        return ;    }    dp[cur][0] = 1;    dp[cur][2] = 0;    dp[cur][1] = 0;    bool ok = 0;    int MIN = inf;    int sum = 0;    for (int i = 0; i < Siz(g[cur]); ++i){        int v = g[cur][i];        if (v != f){            dfs(v,cur);            dp[cur][0] += Min(dp[v][0],Min(dp[v][1],dp[v][2]));            dp[cur][2] += Min(dp[v][0],dp[v][1]);            MIN = Min(MIN, dp[v][0] - dp[v][1]);            sum += dp[v][1];            if (dp[v][0] > dp[v][1]){                dp[cur][1] += dp[v][1];            }            else{                ok = 1;                dp[cur][1] += dp[v][0];            }        }    }    if (!ok){        dp[cur][1] = sum + MIN;    }}int main(){    while(~scanf("%d",&n)){        for (int i = 1; i <= n; ++i)g[i].clear();        for (int i =1; i < n; ++i){            int u,v;            scanf("%d %d",&u, &v);            g[u].push_back(v);            g[v].push_back(u);        }        dfs(1,-1);        printf("%d\n",Min(dp[1][1],dp[1][0]));    }    return 0;}

Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6522 Accepted: 2348

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 Bis 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

51 35 24 33 5

Sample Output

2

Source

USACO 2008 January Gold

[Submit]   [Go Back]   [Status]   [Discuss]


0 0
原创粉丝点击