题目链接:http://poj.org/problem?id=1463

给你一棵树形图,问最少多少个点覆盖所有的边。

可以用树形dp做,任选一点,自底向上回溯更新。

dp[i][0] 表示不选i点 覆盖子树所有边的最少点个数,那选i点的话,那么i的邻接节点都是必选的,所以dp[i][0] += dp[i.son][1]

dp[i][1] 表示选i点 覆盖子树所有边的最少点个数,那么i的邻接点可选可不选(而不是一定不选,看注释样例就知道了),所以dp[i][0] += min(dp[i.son][1], dp[i.son][0])

 //dp
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = ;
vector <int> G[N];
int dp[N][]; void dfs(int u, int p) {
dp[u][] = , dp[u][] = ;
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v == p)
continue;
dfs(v, u);
dp[u][] += dp[v][];
dp[u][] += min(dp[v][], dp[v][]);
}
} int main()
{
int n;
while(~scanf("%d", &n)) {
int u, num, v;
for(int i = ; i <= n; ++i) {
scanf("%d:(%d)", &u, &num);
while(num--) {
scanf("%d", &v);
G[u].push_back(v);
G[v].push_back(u);
}
}
dfs(, -);
printf("%d\n", min(dp[][], dp[][]));
for(int i = ; i < n; ++i)
G[i].clear();
}
return ;
}
/*
13
0:(3) 1 2 3
1:(0)
2:(2) 4 5
3:(2) 6 7
4:(0)
5:(0)
6:(2) 8 9
7:(3) 10 11 12
8:(0)
9:(0)
10:(0)
11:(0)
12:(0)
*/

边完全覆盖,也就是最小点覆盖所有边。

二分图中最大匹配=最小点覆盖

 //dp
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = ;
vector <int> G[N];
int dp[N][]; void dfs(int u, int p) {
dp[u][] = , dp[u][] = ;
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v == p)
continue;
dfs(v, u);
dp[u][] += dp[v][];
dp[u][] += min(dp[v][], dp[v][]);
}
} int main()
{
int n;
while(~scanf("%d", &n)) {
int u, num, v;
for(int i = ; i <= n; ++i) {
scanf("%d:(%d)", &u, &num);
while(num--) {
scanf("%d", &v);
G[u].push_back(v);
G[v].push_back(u);
}
}
dfs(, -);
printf("%d\n", min(dp[][], dp[][]));
for(int i = ; i < n; ++i)
G[i].clear();
}
return ;
}
/*
13
0:(3) 1 2 3
1:(0)
2:(2) 4 5
3:(2) 6 7
4:(0)
5:(0)
6:(2) 8 9
7:(3) 10 11 12
8:(0)
9:(0)
10:(0)
11:(0)
12:(0)
*/

POJ1463 Strategic game (最小点覆盖 or 树dp)的更多相关文章

  1. HDU1054 Strategic Game —— 最小点覆盖 or 树形DP

    题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others)     ...

  2. LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)

    题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...

  3. HDU 1054 Strategic Game (最小点覆盖)【二分图匹配】

    <题目链接> 题目大意:鲍勃喜欢玩电脑游戏,特别是战略游戏,但有时他无法找到解决方案,速度不够快,那么他很伤心.现在,他有以下的问题.他必须捍卫一个中世纪的城市,形成了树的道路.他把战士的 ...

  4. HDU 1054 Strategic Game 最小点覆盖

     最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...

  5. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  6. POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)

    题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...

  7. SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp

    题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...

  8. HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)

    d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...

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

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

随机推荐

  1. 堆Heap

    #pragma once#include <vector> // 小堆template<class T>  //仿函数struct Less{       bool opera ...

  2. 16.Object-C--NSArray数组的排序

    今天我来总结一下NSArray数组的排序方式. NSArray数组的排序有三种方式: 1.简单排序(sortedArrayUsingSelector:) 2.利用block语法(sortedArray ...

  3. IOS中bounds和frame

    * 用bounds和frame来修改尺寸是有一些小区别的 三.isEqual:方法 1> 系统会根据对象isEqual方法的返回值来决定两个对象是否相同 * 比如判断对象a和b是否相同,就会查看 ...

  4. Linux 系统时钟(date) 硬件时钟(hwclock)

    /********************************************************************* * Linux 系统时钟(date) 硬件时钟(hwclo ...

  5. php最新出现的函数

    1. 数据过滤函数 filter_var:  filter_var — Filters a variable with a specified filter 过滤的类型有: Validate filt ...

  6. 【英语】Bingo口语笔记(62) - 生气道歉场景的表达

  7. 【转】Linux设备驱动之I/O端口与I/O内存

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/08/2281367.html 一.统一编址与独立编址 该部分来自于:http://blog.ch ...

  8. Windows 环境搭建cocos2dx 3.x Eclipse的环境

    安装JDK,该步骤网上太多,不再赘述; 安装NDK,同样,直接去Google找到最新的NDK,下载解压到某个盘符根目录即可; 简便起见,使用ADT Bundle,而不要去使用Eclipse的原生包,可 ...

  9. collect my database for test KCF tracker tools

    Path Button used to set dir where avi file saves, set path set video size and start record write to ...

  10. Nginx gzip配置详解

    gzip决定是否开启gzip模块param:on|offexample:gzip on; gzip_buffers 设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间param1:intp ...