POJ3398Perfect Service[树形DP 树的最大独立集变形]
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 1518 | Accepted: 733 |
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
Source
题意:选一些节点,使得每个没被选节点恰好与一个被选的节点相邻
//
// main.cpp
// poj3398
//
// Created by Candy on 10/14/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e4+,INF=1e6;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,u,v;
struct edge{
int v,ne;
}e[N<<];
int es=,h[N];
inline void ins(int u,int v){
es++;
e[es].v=v;e[es].ne=h[u];h[u]=es;
es++;
e[es].v=u;e[es].ne=h[v];h[v]=es;
}
int f[N][];//0 i | 1 fa | 2 son
void dp(int u,int fa){//printf("dp %d %d\n",u,fa);
f[u][]=;f[u][]=;f[u][]=INF;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
dp(v,u);
f[u][]+=min(f[v][],f[v][]);
f[u][]+=f[v][];
f[u][]=min(f[u][],-f[v][]+f[v][]);
}
f[u][]+=f[u][];//when finish calc
//printf("hi %d %d %d %d\n",u,f[u][0],f[u][1],f[u][2]);
}
int main(int argc, const char * argv[]) {
while(true){
n=read();
if(n==) continue;
if(n==-) break;
es=;
memset(h,,sizeof(h));
for(int i=;i<=n-;i++){
u=read();v=read();ins(u,v);
}
dp(,);
printf("%d\n",min(f[][],f[][]));
} return ;
}
POJ3398Perfect Service[树形DP 树的最大独立集变形]的更多相关文章
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
Party at Hali-Bula Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5660 Accepted: 202 ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- 树形DP 树的最小支配集,最小点覆盖与最大独立集
最小支配集: 从V中选取尽量少的点组成一个集合,让V中剩余的点都与取出来的点有边相连. (点) 最小点覆盖: 从V中选取尽量少的点组成一个集合V1,让所有边(u,v)中要么u属于V1,要么v属于V1 ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【XSY2190】Alice and Bob VI 树形DP 树剖
题目描述 Alice和Bob正在一棵树上玩游戏.这棵树有\(n\)个结点,编号由\(1\)到\(n\).他们一共玩\(q\)盘游戏. 在第\(i\)局游戏中,Alice从结点\(a_i\)出发,Bob ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
随机推荐
- sqlserver2008存储过程(比较两个日期大小和获取当前月最大天数的存储过程)
下面简单介绍sqlserver2008两个常用的存储过程 1.比较两个日期大小的存储过程 2.获取当前月份的最大天数的存储过程 1.创建比较两个日期大小的存储过程 1)创建比较两个日期大小的存储过程 ...
- Unity3D 5.x 简单实例 - 发射炮弹
1,下载.安装: http://unity3d.com/cn/get-unity/download/archive 建议直接借助 UnityDownloadAssistant 进行安装,根据需要勾选需 ...
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...
- Bootstrap之字体图标
优点:1.减少请求 2.容易控制样式 所在位置:在下载的bootstrap文件中的fonts文件夹存放字体图标 默认路径为当前目录下,如需修改路径,则需在bootstrap.css中查找font-fa ...
- 解决IE兼容模式问题
IE浏览器从IE8开始添加了兼容模式,开启后会以低版本的IE进行渲染.在浏览网页时候会出现网页显示问题,于是可以在html中加入以下代码来使IE使用固定的渲染模式: <metahttp-equi ...
- Java Web中乱码问题
response.setContentType("text/html;charset=UTF-8"); 用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式.什么编码读取这个 ...
- Mac系统下Android生成keystore
首先打开终端(在搜索里面搜索Te即可出来) 然后输入 cd /Library/Java/Home/bin/ 然后这步很关键,由于我们用的是当前用户,所以没有最高权限,不能在Library文件夹下生成 ...
- IOS开发基础知识--碎片27
1:iOS中的round/ceil/floorf extern float ceilf(float); extern double ceil(double); extern long double c ...
- 【代码笔记】iOS-文字走马灯效果
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- iOS 直播-闪光灯的使用
iOS 直播-闪光灯的使用 应用场景是这样的,最近公司决定做一款直播类的软件. 在开发中就遇到了不曾使用过的硬件功能-闪光灯. 这篇博客将简单介绍一下闪光灯的使用. // // ViewControl ...