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的更多相关文章

  1. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  2. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  3. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  4. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  5. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  6. 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 ...

  7. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  8. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. CreateProcessEx创建进程

    NTSYSCALLAPI NTSTATUS NTAPI NtCreateProcess( OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess ...

  2. shell 删除隐藏文件.svn

    参考:https://blog.csdn.net/zhangxinrun/article/details/6409125 echo "recursively removing .svn fo ...

  3. docker使用记录一日常使用的命令

    docker官网 介绍docker的文档 https://docs.docker.com/install/linux/docker-ce/centos/ centos 安装docker 卸载cento ...

  4. JNDI数据源的配置及使用

    数据源的作用JDBC操作的步骤:  1. 加载驱动程序  2. 连接数据库  3. 操作数据库  4. 关闭数据库,释放连接 也就是说,所有的用户都需要经过此四步进行操作,但是这四步之中有三步对所有人 ...

  5. HDU 1577 WisKey的眼神

    WisKey的眼神 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  6. db2缓冲池

    CREATE BUFFERPOOL DEMOBP IMMEDIATE  SIZE 250 AUTOMATIC PAGESIZE 4 K ; CREATE BUFFERPOOL DEMOBP IMMED ...

  7. android Manifest.xml 文件详解

  8. 【学习总结】Python-3- 类型判断之 isinstance 和 type 的区别

    菜鸟教程-Python3-基本数据类型 关于类型查询: type() 函数:可以用来查询变量所指的对象类型 用 isinstance()函数:判断是否是某个类型 两者的区别: type()不会认为子类 ...

  9. DataX简介

    DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hive.ADS.HBase.TableStore(O ...

  10. Redis-HA

    Redis-HA部署 链接:https://pan.baidu.com/s/1cj5H9snQXqWaC0od1mUuig 提取码:jdqf 复制这段内容后打开百度网盘手机App,操作更方便哦 1. ...