Cell Phone Network
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 ≤ AN; 1 ≤ BN; AB) 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

一开始令f[i][0/1]表示属于/不属于支配集的最优解,后来发现这个方程有问题,如果i不属于支配集,他的儿子可能也不属于支配集而是被儿子的儿子所覆盖,这样子就没办法表示了。换句话说状态表示不全。
         就是一道模板最小支配集,用树形dp实现,注意不能转移的状态用inf表示;
 f[i][0]表示i属于支配集且i的子树全被覆盖情况下,最小的支配集。
    f[i][1]表示i不属于支配集,i被至少一个子节点所覆盖,且i的子树全被覆盖情况下,最小的支配集。
    f[i][2]表示i不属于支配集,i不被任何一个子节点覆盖,且i的子树全被覆盖情况下,最小的支配集。
   有转移方程 f[i][0]=1+SUM{MIN{f[j][0],f[j][1],f[j][2]} }
       f[i][1]=f[jx][0]+SUM{min(f[j][0],f[j][1]) | j!=jx}
       f[i][2]=sum{f[j][1] }  /// 注意inf的状态不能被转移,f[i][1],至少有一个i的儿子是由f[j][0]转移而来的才ok。
  
  

 #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的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 3398 Perfect Service --最小支配集

    题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...

  7. 树形dp(最小支配集)

    http://poj.org/problem?id=3659 #include<iostream> #include<cstring> #include<algorith ...

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

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

  9. 树形DP 树的最小支配集,最小点覆盖与最大独立集

    最小支配集: 从V中选取尽量少的点组成一个集合,让V中剩余的点都与取出来的点有边相连. (点) 最小点覆盖: 从V中选取尽量少的点组成一个集合V1,让所有边(u,v)中要么u属于V1,要么v属于V1 ...

随机推荐

  1. 在Windows控制台应用程序中使用CString

    CString是在windows平台下开发中经常使用的字符串类, CString已从MFC中剥离出来了,可以单独使用,只需引用atlstr.h头文件即可. include "stdafx.h ...

  2. 爬虫-Beautiful Soup模块

    阅读目录 一 介绍 二 基本使用 三 遍历文档树 四 搜索文档树 五 修改文档树 六 总结 一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通 ...

  3. javascript 类型 内存 对象

    var box =0 function test() { alert(box) //全局 }

  4. Leetcode 357

    没用过Leetcode刷题,只能按照自己的想法随便写写了 思路:1.第一位数有9种(除了0)可能,第二位数有9种(除了第一位)可能,第三位数有8种(除了前两位)可能,以此类推...9*8*7*...( ...

  5. unity手势插件《FingerGestures 》使用入门

    什么是FingerGestures? FingerGestures是Unity上,非常热门的一款用于处理用户输入的插件 为什么要使用FingerGestures? 1:它统一了鼠标点击和用户触摸的输入 ...

  6. Eclipse jvm启动参数在哪设置

    学习并转载自https://jingyan.baidu.com/article/624e7459653ca534e8ba5a26.html Java是一门非常受欢迎的编程语言,Java的开发人员多数使 ...

  7. Python的幂运算

    直接用例子说明

  8. Python面试题之Python正则表达式re模块

    一.Python正则表达式re模块简介 正则表达式,是一门相对通用的语言.简单说就是:用一系列的规则语法,去匹配,查找,替换等操作字符串,以达到对应的目的:此套规则,就是所谓的正则表达式.各个语言都有 ...

  9. 如何使用curl进行网页授权

    答:使用curl的-u选项,使用方法如下 curl -u username URL (会提示输入密码)

  10. 为什么说git比svn好

    http://blog.jobbole.com/20069/ git的权限控制,可以借助第三方的工具来实现 也快成使用git的子模块 http://www.cnblogs.com/aga-j/arch ...