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


Sample Output

 

题目大意:

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

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

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

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

这里用贪心法来求:

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

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

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

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
const int INF=0x3f3f3f3f;
using namespace std; int n;
int Index;
int cnt_edge;
int node[];
int fa[];
int vis[];
int DFN[];
int F[]; struct Edge{
int to;
int next;
}E[]; void add_edge(int a,int b)
{
E[cnt_edge].next=node[a];
E[cnt_edge].to=b;
node[a]=cnt_edge++;
} void DFS(int u)
{
DFN[Index++]=u;
for(int i=node[u];i!=-;i=E[i].next)
{
int v=E[i].to;
if(!vis[v])
{
fa[v]=u;
vis[v]=;
DFS(v);
}
}
} int solve()
{
int ans=;
memset(vis,,sizeof(vis));
for(int i=Index-;i>=;i--)
{
int v=DFN[i];
if(!vis[v])
{
if(!F[fa[v]])
{
F[fa[v]]=;
ans++;
}
vis[v]=;
vis[fa[v]]=;
vis[fa[fa[v]]]=;
}
}
return ans;
} int main()
{
scanf("%d",&n);
memset(node,-,sizeof(node));
for(int i=;i<n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
vis[]=;
DFS();
printf("%d\n",solve());
return ;
}

poj-3659 Cell Phone Network(最小支配集+贪心)的更多相关文章

  1. POJ 3659 Cell Phone Network 最小支配集模板题(树形dp)

    题意:有以个 有 N 个节点的树形地图,问在这些顶点上最少建多少个电话杆,可以使得所有顶点被覆盖到,一个节点如果建立了电话杆,那么和它直接相连的顶点也会被覆盖到. 分析:用最少的点覆盖所有的点,即为求 ...

  2. POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法

    POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...

  3. POJ 3659 Cell Phone Network(树的最小支配集)(贪心)

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

  4. 求树的最大独立集,最小点覆盖,最小支配集 贪心and树形dp

    目录 求树的最大独立集,最小点覆盖,最小支配集 三个定义 贪心解法 树形DP解法 (有任何问题欢迎留言或私聊&&欢迎交流讨论哦 求树的最大独立集,最小点覆盖,最小支配集 三个定义 最大 ...

  5. POJ - 3659 Cell Phone Network(树形dp---树的最小点支配集)

    题意:有N个点,N-1条边,任意两点可达,由此形成了一棵树.选取一个点a,它可覆盖自己以及与自己相邻的点,选取尽量少的点a,使得树中所有点都被覆盖,即求树的最小点支配集. 分析: 1.对于每一个点cu ...

  6. POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)

    题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...

  7. POJ 3659 Cell Phone Network (树dp)

    题目链接:http://poj.org/problem?id=3659 给你一个树形图,一个点可以覆盖他周围连接的点,让你用最少的点覆盖所有的点. dp[i][0]表示用i点来覆盖,dp[i][1]表 ...

  8. POJ3659 Cell Phone Network(树上最小支配集:树型DP)

    题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...

  9. POJ 3398 Perfect Service(树型动态规划,最小支配集)

    POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...

随机推荐

  1. (排序)P1177 【模板】快速排序

    题解: 这道题用传统快排(如下所示)的结果就是最后三个点TLE: 如果永远取第一个元素作为枢轴的话,在数组已经有序的情况下每次划分都将得到最坏的结果,时间复杂度退化为O(n^2).因为其中一个子序列每 ...

  2. 自己编写DLL并导出函数

    sub.c #include<windows.h> #include"sub.h" int WINAPI DllMain(_In_ HANDLE _HDllHandle ...

  3. class选择器,外部样式表,选择器优先级

    class选择器: 先在相应标签中设置一个class属性,如class=“class名”.class名{ ……css样式}注:class名以英文字母开头,可以多个标签重复使用.优先级:标签名选择器 & ...

  4. textField 基本属性

    _textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textField.text = str; [_t ...

  5. Linux-异步IO

    1.何为异步IO (1).几乎可以这么认为:异步IO就是操作系统用软件实现的一套中断响应系统. (2).异步IO的工作方法:我们当前进程注册一个异步IO事件(使用signal注册一个信号SIGIO的处 ...

  6. Python 模拟 Base64编码

    Base64编码原理:https://blog.csdn.net/wo541075754/article/details/81734770 def Enbs64(s): # 编码后的结果 result ...

  7. Java正则表达式基础知识整理

    指定为字符串的正则表达式必须首先被编译为此类的实例.然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配.执行匹配所涉及的所有状态都驻留在匹配器中,所以多个 ...

  8. php 使用redis队列简单实用

    简介:队列要遵守先进先出的原则 demo.php <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $arr = ...

  9. nginx调整large_client_header_buffers

    https://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers Syntax: large_c ...

  10. h5集成环信在线客服自定义窗口

    自定义客服窗口从底部弹出 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...