洛穀P2899 [USACO08JAN]行動電話通訊Cell Phone Network

來源:互聯網
上載者:User

標籤:相互   last   article   min   rmi   mini   content   節點   ++   

P2899 [USACO08JAN]行動電話通訊Cell Phone Network題目描述

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 ≤ 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.

John想讓他的所有牛用上手機以便相互交流(也是醉了。。。),他需要建立幾座訊號塔在N塊草地中。已知與訊號塔相鄰的草地能收到訊號。給你N-1個草地(A,B)的相鄰關係,問:最少需要建多少個訊號塔能實現所有草地都有訊號。

輸入輸出格式輸入格式:

 

  • 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

 

輸出格式:

 

  • Line 1: A single integer indicating the minimum number of towers to install

 

輸入輸出範例輸入範例#1:
51 35 24 33 5
輸出範例#1:
2
/*    f[i][0]表示節點i不選但被覆蓋     f[i][1]表示節點i選    f[i][2]表示節點i不選也不被覆蓋*/#include<iostream>#include<cstdio>#define maxn 10010#define INF 2000000000using namespace std;int f[maxn][3],n,num,head[maxn];struct node{    int to,pre;}e[maxn*2];void Insert(int from,int to){    e[++num].to=to;    e[num].pre=head[from];    head[from]=num;}void dfs(int now,int father){    int f0=INF,f2=0,f1=0,w=0,s=0;    for(int i=head[now];i;i=e[i].pre){        int to=e[i].to;        if(to==father)continue;        dfs(to,now);        s=min(f[to][0],f[to][1]);        w+=s;        f0=min(f0,f[to][1]-s);        f1+=min(f[to][1],min(f[to][0],f[to][2]));        if(f2<INF)f2+=f[to][0];    }    f[now][1]=f1+1;f[now][2]=f2;    if(f0==INF)f[now][0]=INF;    else f[now][0]=w+f0;}int main(){    int x,y;    scanf("%d",&n);    for(int i=1;i<n;i++){        scanf("%d%d",&x,&y);        Insert(x,y);        Insert(y,x);    }    dfs(1,0);    int ans=min(f[1][1],f[1][0]);    cout<<ans;}

 

洛穀P2899 [USACO08JAN]行動電話通訊Cell Phone Network

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.