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;
}

最新文章

  1. ASM:《X86汇编语言-从实模式到保护模式》第17章:保护模式下中断和异常的处理与抢占式多任务
  2. Word2013中制作按钮控件
  3. ionic环境搭建及新建项目中的各种问题
  4. 【LeetCode】Product of Array Except Self
  5. [Java基础]java的main函数
  6. ASP.NET 4.5 和 Visual Studio 2012 中的新功能
  7. js实现checkbox的全选/取消
  8. 基础数据结构 之 栈(python实现)
  9. sublime打开文件时自动生成并打开.dump文件
  10. WindowsServer2008配置MySql Proxy
  11. js replace如何实现全部替换
  12. 杭电1513Palindrome
  13. 使用ADO.NET查询和访问数据库
  14. 并发编程(十):AQS
  15. web页面空白,无任何显示
  16. QTP 自动货测试桌面程序-笔记-运行结果中添加截图
  17. 【LeetCode】4. 寻找两个有序数组的中位数
  18. Dapper实现一对多对象关系聚合导航属性
  19. 【DS】排序算法之归并排序(Merge Sort)
  20. 195 Tenth Line

热门文章

  1. upc组队赛14 Bus stop【签到水】
  2. 解决本地工具无法连接服务器上的mysql的问题
  3. JVM运行时区域详解。
  4. git 上传本地代码
  5. APACHE两种域名跳转法简单完成重定向
  6. go module管理依赖包
  7. linux缺頁異常處理--內核空間[v3.10]
  8. while 循环语句举例
  9. 三、bootstrap-treeview
  10. API登录验证