Perfect service(树形dp)
Perfect service(树形dp)
有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻。求服务器的最小数量。n<=10000。
这种类似独立集的树形dp问题,都可以将同一个结点的状态分成几类。这里用\(f[i][0]\)表示i是服务器,\(f[i][1]\)表示i不是服务器,但是i的父亲是服务器。\(f[i][2]\)表示i和i的父亲都不是服务器。
那么就可以写出转移方程:\(f[i][0]=sum(min(f[v][0], f[v][1]))+1\),\(f[i][1]=sum(f[v][2])\),\(f[i][2]=min(f[i][1]-f[v][2]+f[v][0])\)。时间复杂度为\(O(n)\)。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e4+5;
int cntedge, fir[maxn];
struct Edge{
int to, next;
}e[maxn];
void RESET(){ cntedge=0; memset(fir, 0, sizeof(fir)); }
void addedge(int x, int y){
Edge &e1=e[++cntedge];
e1.to=y; e1.next=fir[x]; fir[x]=cntedge;
}
int n, f[maxn][3]; //0:自己是 1:父亲是 2:自己和父亲都不是
\
//也可以保存访问顺序,在外部访问
void dfs(int u, int par){
f[u][0]=1; f[u][1]=0;
f[u][2]=n; int v;
for (int i=fir[u]; i; i=e[i].next){
if ((v=e[i].to)==par) continue;
dfs(v, u);
f[u][0]+=min(f[v][0], f[v][1]);
f[u][1]+=f[v][2];
}
for (int i=fir[u]; i; i=e[i].next){
if ((v=e[i].to)==par) continue;
f[u][2]=min(f[u][2], f[u][1]-f[v][2]+f[v][0]);
}
}
int main(){
int t1=0, t2;
while (~t1&&~scanf("%d", &n)){
RESET();
for (int i=1; i<n; ++i){
scanf("%d%d", &t1, &t2);
addedge(t1, t2); addedge(t2, t1); }
dfs(1, 0);
printf("%d\n", min(min(f[1][0], f[1][1]), f[1][2]));
scanf("%d", &t1);
}
return 0;
}
Perfect service(树形dp)的更多相关文章
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
- UVA - 1218 Perfect Service (树形dp)(inf相加溢出)
题目链接 题意:给你一个树形图,让你把其中若干个结点染成黑色,其余的染成白色,使得任意一个白色结点都恰好与一个黑色结点相邻. 解法比较容易,和树上的最大独立集类似,取一个结点作为树根,对每个结点分三种 ...
- POJ3398Perfect Service[树形DP 树的最大独立集变形]
Perfect Service Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1518 Accepted: 733 De ...
- UVA-1220-Party at Hali-Bula && UVA-1218-Perfect Service(树形DP)
UVA-1220-Party at Hali-Bula 题意: 一个公司员工要举行聚会,要求任意一个人不能和他的直接上司同时到场,一个员工只有一个支系上司,现在求最多有多少人到场,并且方案是否唯一(紫 ...
- lightoj 1201 - A Perfect Murder(树形dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1201 题解:简单的树形dp,dp[0][i]表示以i为根结点不傻i的最多有多少 ...
- Perfect Service [POJ 3398]
Perfect Service 描述 网络由N个通过N-1个通信链路连接的计算机组成,使得任何两台计算机可以通过独特的路由进行通信.如果两台计算机之间存在通信链路,则称这两台计算机是相邻的.计算机的邻 ...
- POJ 1849 - Two - [DFS][树形DP]
Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets t ...
- POJ-3659-最小支配集裸题/树形dp
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7127 Accepted: 254 ...
- UVa 1218 - Perfect Service
/*---UVa 1218 - Perfect Service ---首先对状态进行划分: ---dp[u][0]:u是服务器,则u的子节点可以是也可以不是服务器 ---dp[u][1]:u不是服务器 ...
随机推荐
- L99
You're not obligated to win. You're obligated to keep trying.你不一定要获胜,但你必须不断尝试.He announced an expans ...
- 如何用Mendeley引用目标期刊要求的参考文献格式
如果我们要向目标的杂志期刊投稿,则需要采用该期刊的参考文献格式.我用的mendeley管理文献,不收费且使用方便.那么,我们如何用mendeley引用目标期刊的参考文献呢?以Applied energ ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- bzoj 3881: [Coci2015]Divljak AC自动机
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3881 题解: 这道题我想出了三种做法,不过只有最后一种能过. 第一种: 首先我们把所有的 ...
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- scrollHeight
scrollHeight=显示内容高度+隐藏内容高度 参考: https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollHeight ...
- keepalive安装和配置
1.下载安装包并解压 sudo wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz tar zxvf keepalived ...
- C#某月的第一天和最后一天
1.本月的第一天===>DateTime.Now.AddDays(1 - DateTime.Now.Day);//当前日期减去当前日期和本月一号相差天数 2.本月的最后一天===>Date ...
- flume+kafka+storm+mysql架构设计
前段时间学习了storm,最近刚开blog,就把这些资料放上来供大家参考. 这个框架用的组件基本都是最新稳定版本,flume-ng1.4+kafka0.8+storm0.9+mysql (项目是mav ...
- Hibernate---Hql查询2---
hibernate.cfg.xml配置: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configurati ...