UVA
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
这题难在建立状态
这里面有三种状态的点 1该点是服务器,2该点不是服务器但是父亲节点是(这样的话子节点不能是服务器),3该点不是服务器父亲节点也不是(这样的话子节点必须要有一个是服务器)
状态建好了,转移并不难
dp[u][0]={min(dp[son[u]][0],dp[son[u]][1])}+1;//表示该点是服务器
dp[u][1]={dp[son[u]][2]} //dp[u][1]表示该点不是服务器,父亲节点是服务器
dp[u][2]={dp[son[u]][2]} +min(-dp[son[u]][2],dp[son[u]][0]) //他是由子节点只有一个是服务器转移来的
//
// Created by Zeroxf on 2015-08-09-20.06
// Copyright: (c) 2015 Zeroxf. All rights reserved
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1e4+10;
const int INF = 1e9;
vector <int> G[maxn] ,vertices;
int p[maxn], dp[maxn][3],n;
void dfs(int u,int fa){
p[u] = fa;
vertices.push_back(u);
for( int i =0; i < G[u].size() ; i++){
int v = G[u][i];
if(v!=p[u]) dfs(v,u);
}
}
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
int a,b;
while(cin>>n&&n){
memset(p, 0 ,sizeof p);
memset(dp, 0,sizeof dp);
for(int i=0;i<maxn;i++) G[i].clear();
for( int i =0;i<n-1;i++){
cin>>a>>b;
a--;b--;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(0,-1);
for(int i = vertices.size()-1; i>=0; i--){
int u = vertices[i];
dp[u][0] = 1;dp[u][1] = 0;
for (int j = 0; j< G[u].size(); j++){
int v = G[u][j];
if(v==p[u]) continue;
dp[u][1] += dp[v][2];
dp[u][0] += min(dp[v][0],dp[v][1]);
if(dp[u][1]>=INF) dp[u][1] = INF;
if(dp[u][0]>= INF) dp[u][0] = INF;
}
dp[u][2] = INF;
for(int j=0;j<G[u].size();j++){
int v = G[u][j];
if(v==p[u]) continue;
dp[u][2] = min (dp[u][1]+dp[v][0]-dp[v][2],dp[u][2]);
}
}
cout<<min(dp[0][0],dp[0][2])<<endl;
cin>>n;
}
return 0;
}
UVA的更多相关文章
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- UVA - 1025 A Spy in the Metro[DP DAG]
UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...
随机推荐
- CSS 6种完全居中最佳实践(整理)
2016年4月28日 1.最佳法: .Absolute-Center { width: 50%; height: 50%; overflow: auto; margin: auto; position ...
- upc组队赛15 Supreme Number【打表】
Supreme Number 题目链接 题目描述 A prime number (or a prime) is a natural number greater than 1 that cannot ...
- laravel在路由中设置中间件
//单个 路由 Route::get( 'admin/admin/index' , [ 'middleware' => 'old', 'uses' => 'Admin\AdminContr ...
- vue基础学习一
写一个例子,告诉你VUE的方便之处,就是双向绑定,不需要操作DOM对象,而是操作数据 div中msg 和JS中msg是一一对应的 然后看浏览器中 然后如果我想改变浏览器中值,我在console这么操作 ...
- HDU 1029Ignatius and the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- SpringBoot-技术专区-异步编程
最近在实现一个聚合搜索的需求时,由于需要从五个索引中查询数据,然后再将搜索结果组合返回给前端app展现,显然这个地方不能再用同步的方式来操作了,如果有一个索引查询出现耗时较长,那么其余的请求都会排同步 ...
- Centos7 安装vscode
1.官网下载vscode https://vscode.cdn.azure.cn/stable/0f3794b38477eea13fb47fbe15a42798e6129338/code-1.36.0 ...
- php函数练习20191031
<?php$str="what is you name?";$arr=array('i','s');$arr_1=array('s','i');//是先后替换的.echo s ...
- ARM指令adr adrl ldr mov
ADR是一条小范围的地址读取伪指令,它将基于PC的相对偏移的地址值读到目标寄存器中.格式:ADR register,exper. 编译源程序时,汇编器首先计算当前PC值(当前指令位置)到exper的距 ...
- Hdu-3333 Turning Tree (离线树状数组/线段树)
Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法 HDU-3333 Turing ...