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. shiro 密码如何验证?

    Authentication:身份认证/登录,验证用户是不是拥有相应的身份. Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限:即判断用户是否能做事情. 这里我们主要 ...

  2. 2、css

    web 前端2 CSS   CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 ...

  3. android 写入联系人

    public void testInsert() { ContentValues values = new ContentValues(); /* * 首先向RawContacts.CONTENT_U ...

  4. JVM类加载机制(转)

    原文出自:http://www.cnblogs.com/ityouknow/p/5603287.html 1.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运 ...

  5. cgwin的ssh错误解决办法

    参考博客    http://hi.baidu.com/luckygirl/item/bd00a6d8a05c310d20e25039 方法一(推荐): 修改/etc/passwd文件,在其中加入 s ...

  6. app安全研究

    国内Android App在线漏洞检测平台  腾讯金刚审计系统 http://service.security.tencent.com/kingkong 免费 无限制 腾讯御安全 http://yaq ...

  7. SDUT oj 2610

    /*题目大意:输入一序列n个数字,然后输入m个询问,每个询问包含左边区间和右边区间,还有a和b,问你这个区间内有几个数大于等于a且小于等于b 做法:树状数组,先求出这个区间内有几个数小于a,然后求这个 ...

  8. tab标签 插件 by 腾讯 jianminlu

    /** * @version 0.1 * @author jianminlu * @update 2013-06-19 15:23 */ (function ($) { /** * @name tab ...

  9. nginx和php之间是怎样通信的呢?

    FastCGI原理 FastCGI是一个运用于Http Server和动态脚本语言间通信的接口,多数流行的Http Server都支持FastCGI,包括Apache.Nginx和lighttpd等. ...

  10. 构造函数挨个过 —— String()

    本篇整理JavaScript中构造函数String的相关知识,主要分为以下三个部分: 构造函数String()的作用与使用方式: String()的属性和方法: 字符串对象实例属性和方法: 一 构造函 ...