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 ≤ N; A ≠ B) 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
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(最小支配集+贪心)的更多相关文章
- POJ 3659 Cell Phone Network 最小支配集模板题(树形dp)
题意:有以个 有 N 个节点的树形地图,问在这些顶点上最少建多少个电话杆,可以使得所有顶点被覆盖到,一个节点如果建立了电话杆,那么和它直接相连的顶点也会被覆盖到. 分析:用最少的点覆盖所有的点,即为求 ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- POJ 3659 Cell Phone Network(树的最小支配集)(贪心)
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6781 Accepted: 242 ...
- 求树的最大独立集,最小点覆盖,最小支配集 贪心and树形dp
目录 求树的最大独立集,最小点覆盖,最小支配集 三个定义 贪心解法 树形DP解法 (有任何问题欢迎留言或私聊&&欢迎交流讨论哦 求树的最大独立集,最小点覆盖,最小支配集 三个定义 最大 ...
- POJ - 3659 Cell Phone Network(树形dp---树的最小点支配集)
题意:有N个点,N-1条边,任意两点可达,由此形成了一棵树.选取一个点a,它可覆盖自己以及与自己相邻的点,选取尽量少的点a,使得树中所有点都被覆盖,即求树的最小点支配集. 分析: 1.对于每一个点cu ...
- POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)
题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...
- POJ 3659 Cell Phone Network (树dp)
题目链接:http://poj.org/problem?id=3659 给你一个树形图,一个点可以覆盖他周围连接的点,让你用最少的点覆盖所有的点. dp[i][0]表示用i点来覆盖,dp[i][1]表 ...
- POJ3659 Cell Phone Network(树上最小支配集:树型DP)
题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
随机推荐
- VSFTP 连接时425 Security: Bad IP connecting.报错-----解决方法
当登录FTP时候出现这个报错时.是因为PASV模式的安全检查是开启的(默认是开启的) ftp> ls227 Entering Passive Mode (172,16,101,33,35,58 ...
- Unity3d中渲染到RenderTexture的原理,几种方式以及一些问题
超级搬运工 http://blog.csdn.net/leonwei/article/details/54972653 ---------------------------------------- ...
- mysql初始化数据库建表脚本
set names utf8; set global validate_password.policy=LOW;set global validate_password.length=6;CREATE ...
- mysql 时区问题导致的时间相差14小时
1.mysql 字段名称 类型 begin_time TIME begin_time=08:18:39 2.java数据库连接串 jdbc:mysql://x.x.x.x:3306/y?useUnic ...
- dateutil 2.5.0 is the minimum required version python
问题重现 在运行以下代码时出现了该错误: import pandas as pd import numpy as np 原因与解决 原因是dateutil库版本低于2.5.0,卸载重装即可: pip ...
- Python调用c++可执行程序
1.c++编译程序 #include <iostream> using namespace std; int test() { , b = ; return a+b; } int main ...
- mysql时间类型总结及常用时间函数
日期时间和类型 常用日期和时间类型 字节 year 1 表示年份 值范围:(1901----2155) date ...
- PHP 的变量类型,变量检测
1.PHP的变量类型: 整型 浮点型 字符串 布尔型 数组 对象 null 资源类型 一个变量就是一个盒子,类型可以看做盒子的标签,变量的值就是盒子里的内容 null 是没有类型的空盒子, ...
- 【2017西安邀请赛:A】XOR(线段树+线性基)
前言:虽然已经有很多题解了,但是还是想按自己的理解写一篇. 思路:首先分析题目 一.区间操作 —— 线段树 二.异或操作 —— 线性基 这个两个不难想,关键是下一步的技巧 “或”运算 就是两个数的二进 ...
- C语言I作业博客07
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/9935 我在这个课程的目 ...