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

    http://poj.org/problem?id=3659

    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 ≤ NA ≠ B) there is a sequence of adjacent pastures such that 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

    题目大意:

    John想让他的所有牛用上手机以便相互交流(也是醉了。。。),他需要建立几座信号塔在N块草地中。已知与信号塔相邻的草地能收到信号。给你N-1个草地(A,B)的相邻关系

    问:最少需要建多少个信号塔能实现所有草地都有信号。

    思路:考察树最小支配集问题。可以学习学习别人的博客https://www.cnblogs.com/i-love-acm/p/3558238.html

    最小支配集:从所有顶点中取尽量少的点组成一个集合,使得剩下的所有点都与取出来的点有边相连。顶点个数最小的支配集被称为最小支配集。

    这里用贪心法来求:

    1.以1号点深度优先搜索整棵树,求出每个点在DFS中的编号和每个点的父亲节点编号。

    2.按DFS的反向序列检查,如果当前点既不属于支配集也不与支配集中的点相连,且它的父亲也不属于支配集,将其父亲点加入支配集,支配集个数加1。

    3.标记当前结点、当前结点的父节点(属于支配集)、当前结点的父节点的父节点(与支配集中的点相连)。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <queue>
     8 #include <set>
     9 const int INF=0x3f3f3f3f;
    10 using namespace std;
    11 
    12 int n;
    13 int Index;
    14 int cnt_edge;
    15 int node[10010];
    16 int fa[10010];
    17 int vis[10010];
    18 int DFN[10010];
    19 int F[10010];
    20 
    21 struct Edge{
    22     int to;
    23     int next;
    24 }E[20010];
    25 
    26 void add_edge(int a,int b)
    27 {
    28     E[cnt_edge].next=node[a];
    29     E[cnt_edge].to=b;
    30     node[a]=cnt_edge++;
    31 }
    32 
    33 void DFS(int u)
    34 {
    35     DFN[Index++]=u;
    36     for(int i=node[u];i!=-1;i=E[i].next)
    37     {
    38         int v=E[i].to;
    39         if(!vis[v])
    40         {
    41             fa[v]=u;
    42             vis[v]=1;
    43             DFS(v);
    44         }
    45     }
    46 }
    47 
    48 int solve()
    49 {
    50     int ans=0;
    51     memset(vis,0,sizeof(vis));
    52     for(int i=Index-1;i>=0;i--)
    53     {
    54         int v=DFN[i];
    55         if(!vis[v])
    56         {
    57             if(!F[fa[v]])
    58             {
    59                 F[fa[v]]=1;
    60                 ans++;
    61             }
    62             vis[v]=1;
    63             vis[fa[v]]=1;
    64             vis[fa[fa[v]]]=1;
    65         }
    66     }
    67     return ans;
    68 }
    69 
    70 int main()
    71 {
    72     scanf("%d",&n);
    73     memset(node,-1,sizeof(node));
    74     for(int i=1;i<n;i++)
    75     {
    76         int a,b;
    77         scanf("%d %d",&a,&b);
    78         add_edge(a,b);
    79         add_edge(b,a);
    80     }
    81     vis[1]=1;
    82     DFS(1);
    83     printf("%d
    ",solve());
    84     return 0;
    85 }

     

  • 相关阅读:
    ASP.NET 使用Ajax(转)
    使用Docker,很多坑(之一):在windows中使用
    .NET Core 各种学习资源
    docker-compose.yml配置文件详解(转)
    英雄无敌王国刷将脚本
    Valid format values for declare-styleable/attr tags[转]
    no Session问题,即延迟加载
    适配器模式--Adapter Pattern
    策略模式--Strategy
    装饰模式--Decorator Pattern
  • 原文地址:https://www.cnblogs.com/jiamian/p/11262673.html
Copyright © 2011-2022 走看看