POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集)
Description
A network is composed of N computers connected by N − 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to be adjacent if there is a communication link between them. The neighbors of a computer is the set of computers which are adjacent to it. In order to quickly access and retrieve large amounts of information, we need to select some computers acting as servers to provide resources to their neighbors. Note that a server can serve all its neighbors. A set of servers in the network forms a perfect service if every client (non-server) is served by exactly one server. The problem is to find a minimum number of servers which forms a perfect service, and we call this number perfect service number.
We assume that N (≤ 10000) is a positive integer and these N computers are numbered from 1 to N. For example, Figure 1 illustrates a network comprised of six computers, where black nodes represent servers and white nodes represent clients. In Figure 1(a), servers 3 and 5 do not form a perfect service because client 4 is adjacent to both servers 3 and 5 and thus it is served by two servers which contradicts the assumption. Conversely, servers 3 and 4 form a perfect service as shown in Figure 1(b). This set also has the minimum cardinality. Therefore, the perfect service number of this example equals two.
Your task is to write a program to compute the perfect service number.
Input
The input consists of a number of test cases. The format of each test case is as follows: The first line contains one positive integer, N, which represents the number of computers in the network. The next N − 1 lines contain all of the communication links and one line for each link. Each line is represented by two positive integers separated by a single space. Finally, a 0 at the (N + 1)th line indicates the end of the first test case.
The next test case starts after the previous ending symbol 0. A −1 indicates the end of the whole inputs.
Output
The output contains one line for each test case. Each line contains a positive integer, which is
the perfect service number.
Sample Input
6
1 3
2 3
3 4
4 5
4 6
0
2
1 2
-1
Sample Output
2
1
Http
POJ:https://vjudge.net/problem/POJ-3398
Source
树型动态规划,最小支配集
题目大意
在一棵n个点的树中求一个最小的点集,使得该树上的点满足在这个子集中或与子集中的点相邻,另外,不在该点集中的还满足有且仅与一个在点集中的点相邻。
解决思路
这道题的解决方法与POJ3659差不多,基本的思路可以参照我以前写的文章
那么这道题不同的地方就是非服务器不能连接到多台服务器,所以我们的动态转移方程就要改一改。
(这里先假设读者已经阅读了笔者在上面给出的文章,本文中的各变量意义与上文中的一致)
首先,关于F[u][0]的改变。因为F[u][0]代表是把u作为服务器的情况,所以在本题中它不能由F[v][1]推导得,因为F[v][1]表示v被v的子节点覆盖,若由被u覆盖,与题意相悖。
第二,F[u][2]数组不能从F[v][2]推导得,同样也是上面的原因
第三,F[u][1]现在有且只能被一个子节点覆盖,所以笔者把F[u][1]的计算方式改变了一下(具体方式请看代码,非常巧妙哦!)
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxN=10001;
const int inf=147483647;
int n;
int cnt;
vector<int> E[maxN];
bool vis[maxN];
int F[maxN][5];
void dfs(int u);
int main()
{
while (cin>>n)
{
if (n==-1)
break;
for (int i=1;i<=n;i++)
E[i].clear();
for (int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
E[x].push_back(y);
E[y].push_back(x);
}
int A;
cin>>A;
memset(vis,0,sizeof(vis));
memset(F,0,sizeof(vis));
dfs(1);
cout<<min(F[1][1],F[1][0])<<endl;
if (A==-1)
break;
}
return 0;
}
void dfs(int u)
{
vis[u]=1;
F[u][0]=1;
F[u][2]=0;
F[u][1]=inf;
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i];
if (vis[v]==0)
{
dfs(v);
F[u][0]+=min(F[v][0],F[v][2]);
F[u][2]+=F[v][1];
F[u][1]=min(F[u][1],F[v][0]-F[v][1]);//巧妙之处在这里,这样就可以保证除了选出的F[v][0]-F[v][1]最小的v是从F[v][0]得到
}
}
F[u][1]+=F[u][2];//还有这里,最后加上所有的F[v][1]之和
return;
}
POJ 3398 Perfect Service(树型动态规划,最小支配集)的更多相关文章
- POJ 3398 Perfect Service --最小支配集
题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- POJ3659 Cell Phone Network(树上最小支配集:树型DP)
题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...
- 树型动态规划(树形dp)
树型动态规划就是在“树”的数据结构上的动态规划,树型动态规划是建立在树上的,所以有二个方向: 1.根—>叶:这种题目基本上碰不到 2.叶->根:根的子节点传递有用的信息给根,完后根得出最优 ...
- CJOJ 1010【NOIP2003】加分二叉树 / Luogu 1040 加分二叉树(树型动态规划)
CJOJ 1010[NOIP2003]加分二叉树 / Luogu 1040 加分二叉树(树型动态规划) Description 设 一个 n 个节点的二叉树 tree 的中序遍历为( 1,2,3,-, ...
- CJOJ 2171 火车站开饭店(树型动态规划)
CJOJ 2171 火车站开饭店(树型动态规划) Description 政府邀请了你在火车站开饭店,但不允许同时在两个相连的火车站开.任意两个火车站有且只有一条路径,每个火车站最多有 50 个和它相 ...
随机推荐
- JavaScript Style Guide中文总结
github原址:https://github.com/airbnb/javascript 类型*基本类型:包括string.number.boolean.null.undefined,存储的是值本身 ...
- AbstractHandlerMapping解读
一.AbstractHandlerMapping简介 AbstractHandlerMapping是HandlerMapping的抽象实现,所有的HandlerMapping都继承自AbstractH ...
- 关于微信小程序拒绝授权后,重新授权并获取用户信息
最近公司做了一些有关微信小程序的项目,涉及到授权获取用户基本信息,但是在拒绝授权之后就不会再出现授权窗口: 看网上也有很多人遇到了同样的问题,所以记录下来我的处理方法,供大家和自己学习和记录: 当调用 ...
- python——爬虫&问题解决&思考(四)
继续上一篇文章的内容,上一篇文章中已经将url管理器和下载器写好了.接下来就是url解析器,总的来说这个模块是几个模块中比较难的.因为通过下载器下载完页面之后,我们虽然得到了页面,但是这并不是我们想要 ...
- 【毕业设计】基于Android的家校互动平台开发(内含完整代码和所有文档)——爱吖校推(你关注的,我们才推)
☆ 写在前面 之前答应大家的毕业答辩之后把所有文档贡献出来,现在答辩已过,LZ信守承诺,把所有文档开源到了GitHub(这个地址包含所有的代码和文档以及PPT,外层为简单的代码).还望喜欢的朋友们,不 ...
- [原创] 利用前端+php批量生成html文件,传入新文本,输出新的html文件
本人因为要想自己写个小说网站练练手,在其中遇到的一些问题,将其解决方法总结出来,例如: 1:小说网站存储了大量的小说,每个小说主页都很相似,url不同,不是使用的history属性改写的,所以如果人工 ...
- Spring事务管理—aop:pointcut expression解析
先来看看这个spring的配置文件的配置: <!-- 事务管理器 --> <bean id="transactionManager" class="o ...
- ReactiveCocoa源码解析(二) Bag容器的代码实现
今天博客我接着上篇博客的内容来,上篇博客我们详细的看了ReactiveSwift中的Observer已经Event的代码实现.接下来我们来看一下ReactiveSwift中的结构体Bag的实现.Bag ...
- PHPCMS V9表单向导调用及分页
参考资料如下:v9_form_tlj为你的表单数据表,`flqh`,`title`,`sj`,`username`,`datetime` 为你表单内的字段,page="$_GET" ...
- Phpcms V9缩略图裁剪存在黑边的解决方法
最近用Phpcms v9又碰到一个老问题:在内容页缩略图裁剪的时候出现黑边,这种情况很久没碰到,估计是长宽不同或者会在首页.列表页.内容页不同地方偶然出现的情况,在这里分享下Phpcms V9缩略图裁 ...