Perfect Service
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1518   Accepted: 733

Description

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

Source


题意:选一些节点,使得每个没被选节点恰好与一个被选的节点相邻

以u为根的子树有三种情况
f[u][0]表示选自己(son选不选就随便了)
f[u][1]表示选fa,则son不能被选
f[u][2]表示选son中一个,则fa不能被选
转移时f[u][2]可以用加法优化,就是一堆f[v][1]和一个f[v][0]加起来
//
// main.cpp
// poj3398
//
// Created by Candy on 10/14/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e4+,INF=1e6;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,u,v;
struct edge{
int v,ne;
}e[N<<];
int es=,h[N];
inline void ins(int u,int v){
es++;
e[es].v=v;e[es].ne=h[u];h[u]=es;
es++;
e[es].v=u;e[es].ne=h[v];h[v]=es;
}
int f[N][];//0 i | 1 fa | 2 son
void dp(int u,int fa){//printf("dp %d %d\n",u,fa);
f[u][]=;f[u][]=;f[u][]=INF;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
dp(v,u);
f[u][]+=min(f[v][],f[v][]);
f[u][]+=f[v][];
f[u][]=min(f[u][],-f[v][]+f[v][]);
}
f[u][]+=f[u][];//when finish calc
//printf("hi %d %d %d %d\n",u,f[u][0],f[u][1],f[u][2]);
}
int main(int argc, const char * argv[]) {
while(true){
n=read();
if(n==) continue;
if(n==-) break;
es=;
memset(h,,sizeof(h));
for(int i=;i<=n-;i++){
u=read();v=read();ins(u,v);
}
dp(,);
printf("%d\n",min(f[][],f[][]));
} return ;
}

POJ3398Perfect Service[树形DP 树的最大独立集变形]的更多相关文章

  1. POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)

    Party at Hali-Bula Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5660   Accepted: 202 ...

  2. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  3. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  4. 树形DP 树的最小支配集,最小点覆盖与最大独立集

    最小支配集: 从V中选取尽量少的点组成一个集合,让V中剩余的点都与取出来的点有边相连. (点) 最小点覆盖: 从V中选取尽量少的点组成一个集合V1,让所有边(u,v)中要么u属于V1,要么v属于V1 ...

  5. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  6. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. 【XSY2190】Alice and Bob VI 树形DP 树剖

    题目描述 Alice和Bob正在一棵树上玩游戏.这棵树有\(n\)个结点,编号由\(1\)到\(n\).他们一共玩\(q\)盘游戏. 在第\(i\)局游戏中,Alice从结点\(a_i\)出发,Bob ...

  8. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

  9. UVA - 1218 Perfect Service(树形dp)

    题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...

随机推荐

  1. Top 15 不起眼却有大作用的 .NET功能集

    目录 1. ObsoleteAttribute2. 设置默认值属性: DefaultValueAttribute3. DebuggerBrowsableAttribute4. ??运算符5. Curr ...

  2. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  3. 字符编码笔记:ASCII,Unicode和UTF-8

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节". 再后来,他们又做了一些可以处理 ...

  4. MySQL外键与外键关系说明(简单易懂)

    MySQL主键和外键使用及说明 一.外键约束 MySQL通过外键约束来保证表与表之间的数据的完整性和准确性. 外键的使用条件:  1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后 ...

  5. arcgis地图数据集合

    一.全国的ArcGIS地图SHP格式数据,覆盖的图层信息量基本齐全,除了ArcGIS之外,其他GIS软件(superMap和MapGIS.MapInfo等等)也是用之,适合为GIS开发提供数据素材. ...

  6. 关于web软件信息安全问题防护资料的整理 (一)

    之前意识到了安全问题的重要性,于是就在网上找了一下安全问题的解决办法(主要是web应用以及政府网站方面的),问了一下同学的公司是怎么保证安全的,跟我说用的是shiro安全机制这个貌似好多公司都在用,网 ...

  7. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  8. IOS开发之功能模块--输入框随着键盘的位置移动而移动

    废话不多说,先直接上效果图: 先熟悉一下在Cocoa框架中会用到的key键: 然后直接上Demo的源码截图: 看代码之前,补充说一句,Demo中的文本框以及文本框的背后灰色的View是通过storyb ...

  9. 开发至今,终于开始测试bug,可以省心点了

    今天遇到一个特别奇葩的问题 IOSSDK9.1 Xcode7.1.1 使用表控制器UITableViewController来刷新表时, 之前对表的frame进行的修改,都会被恢复,沾满整个屏幕. 目 ...

  10. [C#6] 0-概览

    1. C#6 新特性图谱 C#6的新特性主要由编译器(“Roslyn”)带来,并不依赖CLR和.NET Framework的升级,我们可以认为这个版本的新语言特性的主要目的为了简化代码书写方式,让我们 ...