树上dp 完美的服务(py)(Perfect Service)(UVa 1218)题解

博客主要介绍了如何使用树形动态规划解决UVa 1218问题,即在一个由N台计算机组成的网络中找到提供完美服务所需的最少服务器数量。给出了样例输入、输出及题目详细解释,并提供了状态转移方程和关键代码片段。
摘要由CSDN通过智能技术生成

累加器传送门:

http://blog.csdn.net/NOIAu/article/details/71775000

题目传送门:

https://vjudge.net/problem/UVA-1218


题目:

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


输入:

The input consists of a number of test cases. The format of each test case is as follows: The rst 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 rst test case.
The next test case starts after the previous ending symbol ‘0’. A ‘-1’ indicates the end of the whole inputs.


输出:

The output contains one line for each test case. Each line contains a positive integer, which is the perfect service number.


样例输入:

6
1 3
2 3
3 4
4 5
4 6
0
2
1 2
-1


样例输出:

2
1


题目大意:

有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻,求服务器的最少数量


很自然地想到用
dp[i][0]表示i是服务器,子节点可以是可以不是
dp[i][1]表示i不是服务器,但i的父亲是服务器,这样一来,i的所有孩子都不是服务器(因为i已经与他爸相连了)
dp[i][2]表示i和他爸都不是服务器,这个意思是,刚好只有一个孩子是服务器
状态转移也很简单


对于dp[i][0]的转移,既然儿子们可以是服务器可以不是服务器,那就取dp[son][0]和dp[son][1]中小的那个值就好了


对于dp[i][1]的转移,既然父亲是服务器,则直接加上儿子们的dp[son][2]就行了(因为son和i都不是服务器,所以对于son来说,就是自己和son的父亲都不是服务器)


对于dp[i][2]的转移,可能会复杂一些,但其实这里有个可以O(1)的转移,非常巧妙
这句代码如下

dp[i][2]=min(d[i][2],dp[i][1]-dp[son][2]+dp[son][0]);

这句话第一次看感觉有点难以理解,其实很容易理解
可以这样看
首先dp[i][1],dp[son][2],dp[son][0]是在计算dp[i][2]的时候已经计算出来了的
然后dp[i][2]表示i点和fa都不为服务器

以自然**等于**i不是服务器,所有son都不是服务器(dp[i][1])减去某一特定的son不是服务器,i不是服务器的可能(dp[son][2],为什么要减呢?因为我枚举son的时候,这个特定的son是要加上选择它的情况的,因为对于i,只能选择一个son,让其是服务器,所以就先让所有都不是服务器,减去该son不是服务器的可能(已经涵盖在dp[i][1]里),加上该son是服务器的可能)


贴上代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#define MAXN 100000+10
#include<queue>
using namespace std;

int n;
vector<int>v[MAXN];
int dp[MAXN][5];
bool visit[MAXN];
bool judge=true;
bool init(){
    scanf("%d",&n);
    if(n==-1) return false;
    for(int i=1;i<=n;i++) v[i].clear();
    memset(visit,false,sizeof(visit));
    int te,mp;
    for(int i=1;i<=n-1;i++){
        scanf("%d%d",&te,&mp);
        v[mp].push_back(te);
        v[te].push_back(mp);
    }
    scanf("%d",&te);
    if(te==-1) judge=false;
    return true;
}

void dpp(int u){
    dp[u][0]=1;
    dp[u][1]=0;
    dp[u][2]=0x7fff;
    visit[u]=true;
    int son=v[u].size();
    queue<int> q;
    for(int i=0;i<son;i++){
        int sonv=v[u][i];
        if(!visit[sonv]){
            dpp(sonv);
            dp[u][0]+=min(dp[sonv][0],dp[sonv][1]);
            dp[u][1]+=dp[sonv][2];
            dp[u][2]=min(dp[u][2],dp[u][1]-dp[sonv][2]+dp[sonv][0]);
        }
    }
}

void print(){
    cout<<min(dp[1][0],dp[1][2])<<endl;
}

int main(){
    while(init()){
        dpp(1);
        print();
        if(!judge) break;
    }
    return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值