zoukankan      html  css  js  c++  java
  • POJ 3659 Cell Phone Network(树的最小支配集)(贪心)

    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 ≤ AN; 1 ≤ BN; AB) 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

    5
    1 3
    5 2
    4 3
    3 5
    

    Sample Output

    2
    

    Source

    【分析】给你一棵树,当你给一个节点染色时,他的相邻节点都会被染上色,问最少选择多少节点染色,可以将整棵树染上。
     这是树的最小支配集模板题,贪心做的。
     
    #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 10000
    typedef 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
    ",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
    ",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
    ",greedy());
        return 0;
    }
  • 相关阅读:
    mmsplayer V2 for android 抢先发布
    mmsplayer v2 windows C 之(mmsplayer_wave )
    mmsplayer v2 java 之(MmsplayerActivity Activity类)
    mmsplayer V2 for windows 发布
    今天休息,明天晚上继续IOS版本mmsplayer V2
    mmsplayer v2 java 之(MmsplayerActivity Activity类)
    mmsplayer V2 for windows 发布
    自定义函数:删除数组B中与数组A重复的值
    LotusScript方法扩展
    MS SQL 2005 SP2新功能
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6666012.html
Copyright © 2011-2022 走看看