(简单)树形dp HOJ 2514 Perfect Service

Perfect Service

My Tags  (Edit)
Source : kaohsiung 2006
Time limit : 3 secMemory limit : 64 M

Submitted : 250, Accepted : 116

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.

(简单)树形dp  HOJ  2514 Perfect Service - 恶魔仁 - 恶魔仁

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

题意:给出一颗树,在上面放一些server,那么相邻的点就能得到资源,但是如果一个点是获取资源的就只能从一个点获取资源,然后问你最少要放置多少个server才能满足上述要求
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


思路:我们用dp[i][0]表示这个节点的子节点没有server,dp[i][1]表示这个节点上面放置了一个server,dp[i][2]表示这个节点的子节点中有一个节点放置了server。那么状态转移方程就是dp[i][0] = sum(dp[j][2]) , dp[i][1] = sum(max(dp[j][1],dp[j][0])+1,dp[i][2] = sum(dp[j][2])-max(dp[j][2]-dp[j][1]);

代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

const int maxn = 10000+10;
int dp[maxn][3] , m , n;
struct Node
{
int v;
Node *next;
}*first[maxn] , edge[maxn];

void init()
{
memset(first,0,sizeof(first));
memset(edge,0,sizeof(edge));
m = 0;
}

void add(int x,int y)
{
edge[++m].v = y;
edge[m].next = first[x];
first[x] = &edge[m];
}

void tp(int x,int fa)
{
dp[x][2] = maxn;
dp[x][1] = 1;
Node *p = first[x];
int sum = 0;
while (p)
{
int y = p->v;
if (y!=fa)
{
tp(y,x);
//dp[x][1] place , dp[x][0] , no vist , dp[x][2] visted by son;
sum += dp[y][2];
}
p = p->next;
}
p = first[x];
dp[x][0] = sum;
while (p)
{
int y = p->v;
if (y!=fa)
{
if (dp[y][1] > dp[y][0]) dp[x][1] += dp[y][0];
else dp[x][1] += dp[y][1];
if (sum-dp[y][2]+dp[y][1] < dp[x][2]) dp[x][2] = sum-dp[y][2]+dp[y][1]; 
}
p = p->next;
}
}



void solve()
{
tp(1,-1);
printf("%d\n",min(dp[1][1],dp[1][2]));
}

int main()
{
while (scanf("%d",&n)==1)
{
init();
for (int i = 0 ; i < n-1 ; ++i)
{
int x , y;
scanf("%d%d",&x,&y);
add(x,y);
}
solve();
scanf("%d",&n);
if (n==-1) return 0;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值