POJ 3659 Cell Phone Network(樹的最小支配集)(貪心)

來源:互聯網
上載者:User

標籤:idt   多少   desc   i++   seq   service   source   stream   mat   

Cell Phone Network
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6781   Accepted: 2429

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

51 35 24 33 5

Sample Output

2

Source

USACO 2008 January Gold【分析】給你一棵樹,當你給一個節點染色時,他的相鄰節點都會被染上色,問最少選擇多少節點染色,可以將整棵樹染上。 這是樹的最小支配集模板題,貪心做的。 
#include <iostream>#include <cstring>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <time.h>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define inf 10000000#define mod 10000typedef long long ll;using namespace std;const int N=2e4+10;const int M=50000;int in[N],vis[N];int n,m,k,tim=0;int newpos[N],p[N];vector<int>vec[N],edg[N];bool s[N],se[N];void dfs(int u,int fa){    newpos[tim++]=u;   // printf("%d %d\n",tim-1,newpos[tim-1]);    for(int i=0;i<edg[u].size();i++){        int v=edg[u][i];        if(v!=fa){            p[v]=u;            dfs(v,u);        }    }}int greedy(){    int ans=0;    int i;    for(i=n-1; i>=0; i--)    {        int t=newpos[i];        //printf("%d %d %d\n",t,p[t],p[p[t]]);        if(!s[t])        {            if(!se[p[t]])            {                se[p[t]]=true;                ans++;            }            s[t]=true;            s[p[t]]=true;            s[p[p[t]]]=true;        }    }    return ans;}int main(){    int u,v,ans=0;;    scanf("%d",&n);    for(int i=1; i<n; i++)    {        scanf("%d%d",&u,&v);        edg[u].push_back(v);        edg[v].push_back(u);    }    dfs(1,0);    printf("%d\n",greedy());    return 0;}

 

POJ 3659 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.