Perfect Service
题意:
n个节点树,在一个节点放上一台服务器可以给相邻的其他各点提供服务且一个节点只能接受一台服务器,求使n个节点都被服务放的服务器的最小数量。
分析:
不算太难,状态想的差不多,但是考虑不全面状态方程写错了,要多练啊!!!状态好想,一个节点服务器放法有3中,放在自身,放在父节点,放在孩子节点
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 10010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int used[],dp[N][],n;
vector<int>e[N];
void dfs(int root){
used[root]=;
for(int i=;i<e[root].size();++i){
int son=e[root][i];
if(used[son])continue;
dfs(son);
dp[root][]+=min(dp[son][],dp[son][]);//自己为服务器
dp[root][]+=dp[son][];//以父节点为服务器
dp[root][]=min(dp[root][],dp[son][]-dp[son][]);
}
dp[root][]+=dp[root][];//以子节点为服务器
}
int main()
{
while(~scanf("%d",&n)){
if(n==-)break;
if(n==)continue;
for(int i=;i<=n;++i){
e[i].clear();
dp[i][]=;
dp[i][]=;
dp[i][]=INF;
}
int u,v;
for(int i=;i<n-;++i)
{
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
memset(used,,sizeof(used));
dfs();
printf("%d\n",min(dp[][],dp[][]));
}
return ;
}
dp[root][0]+=min(dp[son][0],dp[son][1]);//自己为服务器
dp[root][1]+=dp[son][2];//以父节点为服务器
dp[root][2]=min(dp[root][2],dp[son][0]-dp[son][2]);
Perfect Service的更多相关文章
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- Perfect Service [POJ 3398]
Perfect Service 描述 网络由N个通过N-1个通信链路连接的计算机组成,使得任何两台计算机可以通过独特的路由进行通信.如果两台计算机之间存在通信链路,则称这两台计算机是相邻的.计算机的邻 ...
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
- UVa 1218 - Perfect Service
/*---UVa 1218 - Perfect Service ---首先对状态进行划分: ---dp[u][0]:u是服务器,则u的子节点可以是也可以不是服务器 ---dp[u][1]:u不是服务器 ...
- Perfect service(树形dp)
Perfect service(树形dp) 有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻.求服务器的最小数量.n<=10000. ...
- POJ 3398 Perfect Service --最小支配集
题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...
- 【POJ】3398 Perfect Service
1. 题目描述某树形网络由$n, n \in [1, 10^4]$台计算机组成.现从中选择一些计算机作为服务器,使得每当普通计算机恰好与一台服务器连接(并且不超过一台).求需要指定服务器的最少数量 2 ...
- UVA - 1218 Perfect Service (树形DP)
思路:dp[i][0]表示i是服务器:dp[i][1]表示i不是服务器,但它的父节点是服务器:dp[i][2]表示i和他的父亲都不是服务器. 转移方程: d[u][0] += min(d[ ...
- UVa 1218 - Perfect Service(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- aChartEngine图表显示
android的数据报表显示 从图中,我们可以看出,绘制一个图表我们其实,我们只需要理解三个概念 1,ChartFactory ,传入XYMutilpleSeriesRenderer,XYMutilp ...
- http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html(重要)
http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html
- lintcode 中等题:subSets 子集
题目 子集 给定一个含不同整数的集合,返回其所有的子集 样例 如果 S = [1,2,3],有如下的解: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], ...
- Dictionary<Key,Value>的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- AC题目简解-线段树
线段树: http://www.notonlysuccess.com/index.php/segment-tree-complete/鉴于notonlysuccess大牛的博客对于题目的思路写的很简陋 ...
- openfire+spark+smack实现即时通讯
近公司项目需要用到即时通讯功能,经过调研发现openfire+spark+smack可以实现.在网上找了很久,资料都十分有限,即使有些朋友实现了也说的不清不楚.于是决定自己研究,耗时一周的时间实现了文 ...
- 车牌识别LPR系统系列文章汇总
这里的LPR的的几篇文章是之前项目的一些相关资料的整理,涉及实验室内部的资料就没有放上来,希望能对想了解这方面的同学,有所帮助,那怕了解个大概也好.知道整体的思路就好.当初就是一个人瞎摸索,走了很多的 ...
- cololection
package cn.bjsxt.col; /** * 简化迭代器原理 * hasNext * next * @author Administrator * */ public class MyArr ...
- iOSbase64
ios中使用BASE64进行加密和解密的方法也很简单,可以直接用google-toolbox-for-mac的GTMBase64.h来实现google-toolbox-for-mac的对应地址如下: ...
- TeeChart的X轴,使用伪装的时间
TeeChart曲线的X轴是时间,但是频率很高.没法完全显示. 例如,一秒钟有2000个点,那么点与点的间隔为0.5毫秒. 使用TChart类的GetAxisLabel事件, 函数手册上对此事件的解释 ...