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;
    }
  • 相关阅读:
    DataAdapter的Fill方法(转)
    C#读取xml节点数据方法小结
    Service-Oriented Architecture,SOA(转)
    SOAP详解(转)
    WebService到底是什么?(转)
    只需要2个工具,百度云盘大文件就能用迅雷和IDM下载
    刘铁猛-深入浅出WPF-系列资源汇总
    WCF书籍
    书籍:wpf学习书籍介绍
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器(转)
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6666012.html
Copyright © 2011-2022 走看看