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 ...
随机推荐
- [题解]Magic Line-计算几何(2019牛客多校第三场H题)
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...
- js设置cookies
//写入cookies的方法 function setCookie(name, value, seconds) { seconds = seconds || 0; //seconds有值就直接赋值,没 ...
- Redis 系列(04-2)Redis原理 - 内存回收
目录 Redis 系列(04-2)Redis原理 - 内存回收 Redis 系列目录 1. 过期策略 1.1 定时过期(主动淘汰) 1.2 惰性过期(被动淘汰) 1.3 定期过期 2. 淘汰策略 2. ...
- Hibernate入门教程(二):Hibernate核心API
1.Configuraction onfiguration configuration = new Configuration(); configuration.configure(); 到src下面 ...
- 解惑结构体与结构体指针(struct与typedef struct在数据结构的第一道坎)
/* 数据结构解惑01 在数据结构中会看到 typedef struct QNode { QElemType data; //数据域 struct QNode *next; //指针域 }QNode ...
- Logstash,Fluentd, Logtail对比伤害
摘要: 针对主流日志采集客户端(Logstash,Fluentd,以及日志服务客户端Logtail)进行功能.性能和稳定性测评 日志收集的场景 DT时代,数以亿万计的服务器.移动终端.网络设备每天产生 ...
- go module管理依赖包
go mod 最大的好处就是摆脱了GOPATH这个限制,在除了GOPATH以外的目录下也能开展你的项目 go mod使用: 1,确保你的go版本是1.1以上 2,创建一个项目目录example,并添加 ...
- maxim - Android UI压力测试
项目介绍 项目地址:https://github.com/zhangzhao4444/Maxim 与monkey对比优势: 快 稳:只进行有意义的操作,防误点状态栏,不会乱断网.卸载应用 支持脱机运行 ...
- Echart报 [MODULE_MISS]"echarts/config的错
echarts插件的引入有两种方式 项目用到Echarts插件,时下比较流行的是模块化包引入,但是很悲催的是楼主用的是标签式引入,所以从官网copy来的代码总是报一个 [MODULE_MISS]&qu ...
- js转换成布尔类型boolean
/** * js转换成布尔值 * a.转换方法:Boolean(var) * b.数字转换成布尔,除了0与NaN,其余都是true * c.字符串转换成布尔,除了空串"",其余都是 ...