POJ-3659-最小支配集裸题/树形dp
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7127 | Accepted: 2549 |
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
5
1 3
5 2
4 3
3 5
Sample Output
2
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
int f[][];
vector<int>g[];
void dfs(int u,int fa)
{
f[u][]=;
f[u][]=;
int sum=,inc=inf;
bool flag=;
for(int i=;i<g[u].size();++i){
int v=g[u][i];
if(v==fa) continue;
dfs(v,u);
if(f[v][]<=f[v][]){
sum+=f[v][];
flag=;
}
else{
sum+=f[v][];
inc=min(inc,f[v][]-f[v][]);
}
f[u][]+=min(f[v][],min(f[v][],f[v][]));
if(f[v][]!=inf&&f[u][]!=inf) f[u][]+=f[v][];///inf表示当前节点不会出现此状态
else f[u][]=inf;
}
if(inc==inf && flag==) f[u][]=inf;
else{
f[u][]=sum;
if(!flag) f[u][]+=inc;
}
if(f[u][]==) f[u][]=inf;
}
int main()
{
int n,i,j,k,u,v;
while(cin>>n){
for(i=;i<n;++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(,);
cout<<min(f[][],f[][])<<endl;
for(i=;i<=n;++i) g[i].clear();
}
return ;
}
POJ-3659-最小支配集裸题/树形dp的更多相关文章
- 求树的最大独立集,最小点覆盖,最小支配集 贪心and树形dp
目录 求树的最大独立集,最小点覆盖,最小支配集 三个定义 贪心解法 树形DP解法 (有任何问题欢迎留言或私聊&&欢迎交流讨论哦 求树的最大独立集,最小点覆盖,最小支配集 三个定义 最大 ...
- POJ3659 Cell Phone Network(树上最小支配集:树型DP)
题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...
- 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 ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- POJ 3398 Perfect Service --最小支配集
题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...
- 树形dp(最小支配集)
http://poj.org/problem?id=3659 #include<iostream> #include<cstring> #include<algorith ...
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- 树形DP 树的最小支配集,最小点覆盖与最大独立集
最小支配集: 从V中选取尽量少的点组成一个集合,让V中剩余的点都与取出来的点有边相连. (点) 最小点覆盖: 从V中选取尽量少的点组成一个集合V1,让所有边(u,v)中要么u属于V1,要么v属于V1 ...
随机推荐
- Code signing is required for product type 'Application' in SDK 'iOS 11.2'
在打包的时候出现这样一个错误,Code signing is required for product type 'Application' in SDK 'iOS 11.2' ,就是说代码签名证书 ...
- javascript面向对象笔记(一)
ECMAscript对象(以下简称对象): ECMA-262把对象定义为:无序属性的集合,其属性可以包含基本值.对象或者函数. 对象的每个属性或方法都有一个名字,而每个名字都映射到一个值.值可以是数据 ...
- Hadoop 2.0 上深度学习的解决方案
原文连接:https://www.paypal-engineering.com/tag/data-science/ 摘要:伴随着数据的爆炸性增长和成千上万的机器集群,我们需要使算法可以适应在如此分布的 ...
- android studio 模拟器中文乱码
这是因为编码格式不统一导致的,在android studio的build.gradle加入默认编码声明就可以了 compileOptions.encoding = "GBK" 参考
- spark[源码]-SparkEnv执行环境创建
sparkEnv概述 sparkEnv是spark的执行环境,其中包括众多与Executor执行相关的对象.在local模式下Driver会创建Executor,local-cluster部署模式或者 ...
- 如何在idea中设置 jsp 内容修改以后,立即生效而不用重新启动服务?
点击 run---->edit configuration--->
- c++对txt文件的读取与写入
转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...
- lvds split两channel输出到一个屏显示
转:https://blog.csdn.net/changqing1990/article/details/81128552 其实之前写过LCD/LVDS的一些时序的基本概念<与LCD移植相关的 ...
- CF1153E Serval and Snake(交互题)
题目 CF1153E Serval and Snake 很有意思的一道交互题 做法 我们观察到,每次查询一行,当这一行仅包含一端是返回的答案是奇数 根据这个性质查询每一行每一列,我们大体能知道两端的位 ...
- mapper的namespace
一般情况下mapper的namespace能随便写,不重复即可, 但如果希望使用mybatis动态代理的接口,就需要namespace中的值和需要对应的Mapper(dao)接口的全路径一致.例如:c ...