POJ - 3659 Cell Phone Network(树形dp---树的最小点支配集)
题意:有N个点,N-1条边,任意两点可达,由此形成了一棵树。选取一个点a,它可覆盖自己以及与自己相邻的点,选取尽量少的点a,使得树中所有点都被覆盖,即求树的最小点支配集。
分析:
1、对于每一个点cur,要想使其被覆盖,有三种情况:
dp[cur][0]---在该点建立塔
dp[cur][1]---在该点的子结点建立塔
dp[cur][2]---在该点的父结点建立塔
2、对于点cur的子结点x,要使其被覆盖:
(1)dp[cur][0] += Min(Min(dp[x][0], dp[x][1]), dp[x][2]);
在cur处建塔的情况下,x可建塔,x的子节点可建塔,x的父结点即cur建塔,三者取最小值,并累加。
(2)dp[cur][2] += Min(dp[x][0], dp[x][1]);
在cur的父结点建塔的情况下,x可建塔,x的子结点可建塔,两者取最小值,并累加。
(3)dp[cur][1] += Min(dp[x][0], dp[x][1]);
在cur的子结点建塔的情况下,至少需要cur的一个子结点建塔cur才能被覆盖,所以对于每一个子结点x,x可建塔,x的子结点可建塔,两者取最小值
与此同时,记录在取最小值的情况下,cur是否有能建塔的子结点,若没有,需要将cur的一个子结点变成可建塔,选取abs(dp[x][1] - dp[x][0])最小的子结点x改变即可。
3、dp[cur][0]最后要加1,因为在cur点要建塔。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int N;
vector<int> v[MAXN];
int dp[MAXN][5];
void dfs(int cur, int father){
memset(dp[cur], 0, sizeof dp[cur]);
int len = v[cur].size();
int dif = INT_INF;
bool ok = false;
for(int i = 0; i < len; ++i){
int x = v[cur][i];
if(x == father) continue;
if(dp[x][0] == INT_INF) dfs(x, cur);
dp[cur][0] += Min(Min(dp[x][0], dp[x][1]), dp[x][2]);
dp[cur][2] += Min(dp[x][0], dp[x][1]);
dif = Min(dif, abs(dp[x][1] - dp[x][0]));
if(dp[x][0] < dp[x][1]){
ok = true;
dp[cur][1] += dp[x][0];
}
else{
dp[cur][1] += dp[x][1];
}
}
++dp[cur][0];
if(!ok) dp[cur][1] += dif;
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N - 1; ++i){
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
memset(dp, INT_INF, sizeof dp);
dfs(1, -1);
printf("%d\n", Min(dp[1][0], dp[1][1]));
return 0;
}
POJ - 3659 Cell Phone Network(树形dp---树的最小点支配集)的更多相关文章
- 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 最小支配集模板题(树形dp)
题意:有以个 有 N 个节点的树形地图,问在这些顶点上最少建多少个电话杆,可以使得所有顶点被覆盖到,一个节点如果建立了电话杆,那么和它直接相连的顶点也会被覆盖到. 分析:用最少的点覆盖所有的点,即为求 ...
- POJ 3659 Cell Phone Network(树的最小支配集)(贪心)
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6781 Accepted: 242 ...
- 树形DP 树的最小支配集,最小点覆盖与最大独立集
最小支配集: 从V中选取尽量少的点组成一个集合,让V中剩余的点都与取出来的点有边相连. (点) 最小点覆盖: 从V中选取尽量少的点组成一个集合V1,让所有边(u,v)中要么u属于V1,要么v属于V1 ...
- 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]表 ...
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
Party at Hali-Bula Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5660 Accepted: 202 ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
随机推荐
- C语言三种整数类型
1,int 是 C 语言的基本整数类型,可以满足我们处理一般数据的需求. C 语言还提供了四个可以修饰 int 的关键字:short.long.signed,以及 unsigned. 利用这四个关键字 ...
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
- eos 智能合约开发体验
eos编译安装 eos 特性 数据存储 eos投票智能合约开发 eos投票智能合约部署测试 注意避坑 eos编译安装 ERROR: Could not find a package configura ...
- 【pwnable.tw】 seethefile
一开始特别懵的一道题. main函数中一共4个功能,openfile.readfile.writefile.closefile. 其中,在最后退出时有一个明显的溢出,是scanf("%s&q ...
- Koa微信公众号开发
微信开发者模式开启需要服务器域名合法并且把接口配置好,这个接口是接通的关键,接通后微信后台的菜单设置功能,客服功能会失效,需要开发者自定义菜单和智能客服界面,并且接通后可以调用微信网页内部的定位分享等 ...
- Ceph 概念理解
简介 Ceph是一个可靠地.自动重均衡.自动恢复的分布式存储系统,根据场景划分可以将Ceph分为三大块,分别是对象存储.块设备存储和文件系统服务. 在虚拟化领域里,比较常用到的是Ceph的块设备存储, ...
- C. Basketball Exercise dp
C. Basketball Exercise time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 每天一点点之python - 基础语法
1.字符串的拼接 'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125) 输出结果如下: 可以通过和c语言一样,也可以通过format()来实现 2.简单运 ...
- POJ 2823:Sliding Window 单调队列
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 48930 Accepted: 14130 ...
- idea开发springboot 的mysql数据库连接问题
今天在家用idea进行springboot开发,前面一些坑相对避免了,但是到数据库这块总是连接不上,报错主要是: Access denied for user 'root'@'localhost' ( ...