(树形DP) poj 3398

Perfect Service
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1393   Accepted: 679

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 byexactly 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

题意】:给出一棵树,每个节点代表一台电脑,现在需要选择一些电脑作为服务器。如果一台电脑被选作服务器,那么它和和它相邻的电脑都会被激活,非服务器的电脑不能同时被多台服务器激活。问最少要选多少台电脑作为服务器使得所有电脑被激活。

【题解】:树dp,树的最小支配集变形。
               设状态
                  dp[u][0] 以u为根且u为服务器且整棵子树都被激活的最小代价。
                  dp[u][1] 以u为根且u被某个儿子结点激活且整棵子树都被激活的最小代价。
                  dp[u][2] 以u为根且u被父亲结点激活且整棵子树都被激活的最小代价。
 
               转移 (v 为 u 的儿子)
                  dp[u][0] = ∑min(dp[v][0], dp[v][2]) + 1;
                  dp[u][1] = min(sum - dp[v][1] + dp[v][0]), sum = ∑dp[v][1];
                  dp[u][2] = ∑dp[v][1];
               
               任取一个结点为root进行树dp, ans = min(dp[root][0], dp[root][1]).

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#define INF 100000000
using namespace std;
vector<int> e[10005];
int n,dp[10005][3];
void init()
{
    for(int i=1;i<=n;i++)
        e[i].clear();
}
void dfs(int u,int father)
{
    dp[u][0]=1;
    dp[u][1]=INF;
    dp[u][2]=0;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v==father)
            continue;
        dfs(v,u);
        dp[u][0]+=min(dp[v][0],dp[v][2]);
        dp[u][2]+=dp[v][1];
    }
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v==father)
            continue;
        dp[u][1]=min(dp[u][2]-dp[v][1]+dp[v][0],dp[u][1]);
    }
}
int main()
{
    int temp;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
            e[y].push_back(x);
        }
        dfs(1,-1);
        printf("%d\n",min(dp[1][0],dp[1][1]));
        scanf("%d",&temp);
        if(temp==-1)
            break;
    }
    return 0;
}

  

posted @ 2015-05-14 11:44  waterfull  阅读(209)  评论(0编辑  收藏  举报