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. ES7之async/await

    async 是 ES7 才有的与异步操作有关的关键字. async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数. async function helloAsync(){ ...

  2. hdu1312题解

    这道题从名称来看看不出什么. 所以我们先读一下题干 There is a rectangular room, covered with square tiles. Each tile is color ...

  3. 使用classList和dataset实现tab切换

    显示效果: 代码实现: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. [转]Log4j使用总结

    Log4j使用总结   一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器. ...

  5. (2)关于opencv解压

    关于opencv解压,一定不能解压到你的C盘的 ProgramFile(x86)中,不然,你肯定不会成功,你要放在C盘的其他文件夹,或者是别的盘中 就是因为这一个错误,我弄了一天,哎哎,时间宝贵啊

  6. POJ - 1061 扩展欧几里德算法+求最小正整数解

    //#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) #inclu ...

  7. css清除select的下拉箭头样式

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  8. iTOP-4418开发板TF卡烧写-引导uboot

    基于迅为iTOP-4418开发板 将 TF 卡接入开发板,将拨码开关设置为 TF 卡启动,进入 uboot 模式,如下图所示. 如下图所示,使用命令“fastboot”,接着就可以通过 OTG 给 e ...

  9. MySQL--SHOW TABLE STATUS命令

    show table status 获取表的信息 来自:http://blog.csdn.net/java2000_wl/article/details/7935035

  10. h5 移动端在阻止touchstart的默认事件时报错

    h5 移动端在阻止touchstart的默认事件时报错 解决办法, 可以添加 *{ touch-action: none;}即可消除错误