POJ - 3659 Cell Phone Network(樹形dp---樹的最小點支配集)

來源:互聯網
上載者:User

標籤:dfs   節點   子節點   lib   lin   als   tac   [1]   else   

題意:有N個點,N-1條邊,任意兩點可達,由此形成了一棵樹。選取一個點a,它可覆蓋自己以及與自己相鄰的點,選取盡量少的點a,使得樹中所有點都被覆蓋,即求樹的最小點支配集。

分析:

1、對於每一個點cur,要想使其被覆蓋,有三種情況:

dp[cur][0]---在該點建立塔

dp[cur][1]---在該點的子結點建立塔

dp[cur][2]---在該點的父結點建立塔

2、對於點cur的子結點x,要使其被覆蓋:

(1)dp[cur][0] += Min(Min(dp[x][0], dp[x][1]), dp[x][2]);

在cur處建塔的情況下,x可建塔,x的子節點可建塔,x的父結點即cur建塔,三者取最小值,並累加。

(2)dp[cur][2] += Min(dp[x][0], dp[x][1]);

在cur的父結點建塔的情況下,x可建塔,x的子結點可建塔,兩者取最小值,並累加。

(3)dp[cur][1] += Min(dp[x][0], dp[x][1]);

在cur的子結點建塔的情況下,至少需要cur的一個子結點建塔cur才能被覆蓋,所以對於每一個子結點x,x可建塔,x的子結點可建塔,兩者取最小值

與此同時,記錄在取最小值的情況下,cur是否有能建塔的子結點,若沒有,需要將cur的一個子結點變成可建塔,選取abs(dp[x][1] - dp[x][0])最小的子結點x改變即可。

3、dp[cur][0]最後要加1,因為在cur點要建塔。

#pragma comment(linker, "/STACK:102400000, 102400000")#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define Min(a, b) ((a < b) ? a : b)#define Max(a, b) ((a < b) ? b : a)const double eps = 1e-8;inline int dcmp(double a, double b){    if(fabs(a - b) < eps) return 0;    return a > b ? 1 : -1;}typedef long long LL;typedef unsigned long long ULL;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const LL LL_INF = 0x3f3f3f3f3f3f3f3f;const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1e9 + 7;const double pi = acos(-1.0);const int MAXN = 10000 + 10;const int MAXT = 10000 + 10;using namespace std;int N;vector<int> v[MAXN];int dp[MAXN][5];void dfs(int cur, int father){    memset(dp[cur], 0, sizeof dp[cur]);    int len = v[cur].size();    int dif = INT_INF;    bool ok = false;    for(int i = 0; i < len; ++i){        int x = v[cur][i];        if(x == father) continue;        if(dp[x][0] == INT_INF) dfs(x, cur);        dp[cur][0] += Min(Min(dp[x][0], dp[x][1]), dp[x][2]);        dp[cur][2] += Min(dp[x][0], dp[x][1]);        dif = Min(dif, abs(dp[x][1] - dp[x][0]));        if(dp[x][0] < dp[x][1]){            ok = true;            dp[cur][1] += dp[x][0];        }        else{            dp[cur][1] += dp[x][1];        }    }    ++dp[cur][0];    if(!ok) dp[cur][1] += dif;}int main(){    scanf("%d", &N);    for(int i = 0; i < N - 1; ++i){        int a, b;        scanf("%d%d", &a, &b);        v[a].push_back(b);        v[b].push_back(a);    }    memset(dp, INT_INF, sizeof dp);    dfs(1, -1);    printf("%d\n", Min(dp[1][0], dp[1][1]));    return 0;}

  

POJ - 3659 Cell Phone Network(樹形dp---樹的最小點支配集)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.