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 ...
随机推荐
- c# 读取和写入excel数据
1. 读取 DataTable GetDataFromExcelByConn(bool hasTitle = false){ OpenFileDialog openFile = new Open ...
- 关于deepin下安装ssh以后root用户登陆报错的解决
最近刚刚接触到deepin,觉得,wow,除了mac,还有这么好看的非win系统,而且第测出那个Linux,宽容度很高,非常适合我这种比较喜欢折腾的人,于是下载了deepin15版本并将其当作虚拟机成 ...
- 对象与json字符串相互转化
在java编程中,json字符串和对象的相互转化十分常用,下面我们就对象如何转化为json字符串以及json字符串如何转化为对象进行简要介绍,以便在代码中能方便使用. 1.依赖 本次介绍的方法依赖ja ...
- leetcode.双指针.524通过删除字母匹配到字典里最长单词-Java
1. 具体题目 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符串.如果答案不存在,则返回空 ...
- [模板]网络最大流 & 最小费用最大流
我的作业部落有学习资料 可学的知识点 Dinic 模板 #define rg register #define _ 10001 #define INF 2147483647 #define min(x ...
- python第三方库安装失败处理方法
各位道友,是不是在使用pip 命令安装第三方库遇到了以下情形呢? 这种情况可真让人头疼啊..经过几番周折,终于找到了认为最有效的解决方法 首先 先把要安装的包下载下来,不管用什么方式 在这里我用的迅雷 ...
- vue - blog开发学7
将基本的项目部署到linux上(前后台只是实现了基本的功能,本次只是记录一些基本的开发流程,完善,等后续) 1.linux环境准备(我用的是阿里云服务器) ①jre.mysql,Nginx基本上这些就 ...
- 【笔记目录2】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总
当前标签: ASP.NET Core快速入门 共2页: 上一页 1 2 任务27:Middleware管道介绍 GASA 2019-02-12 20:07 阅读:15 评论:0 任务26:dotne ...
- Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力
题意及思路:https://www.cnblogs.com/Als123/p/11061147.html 代码: #include <bits/stdc++.h> #define LL l ...
- java一个数组的内存图