UVa-1228 Perfect Service

Perfect Service
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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

题面有点不清楚,这是紫书的例题9-14。大意是:有n台机器组成树状结构,挑选一些机器安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻,求服务器最少数量。

分析:照书上说,

d(u,0)表示u是服务器,每个子结点可以是服务器也可以不是。

d(u,1)表示u不是服务器,但u的父亲是服务器(可得u的儿子必定不是服务器)。

d(u,2)表示u和u的父亲都不是服务器(可得u恰好有一个儿子为服务器)。

状态转移也不难得出 d(u,0)=sum { min ( d(v,0),d(v,1) ) }+1;

d(u,1)=sum( d(v,2) );

d(u,2)可以直接枚举计算,书上说复杂度为O(k^2),因此提出了一个O(k)的解决方法——

d(u,2)=min( d(u,1)-d(v,2)+d(v,0) );

我的理解是:d(v,0)表示u的子结点v是服务器,原本枚举做法是查找子结点中是服务器的d(v,0)与不是服务器的所有子结点d(v,2)加起来,取所有情况中最小的。此处d(u,1)-d(v,2)+d(v,0)表示的是意思与枚举是一致的,方便理解可以将两种方案相等部分取出来:

d(u,1)-d(v,2)=sum(d(v1,2)) 此处v是恰好选择d(v,0)为子结点服务器,为v1则是代表除去v以外其他的u的子结点。

d(u,1)表示u不是服务器,父亲是服务器,儿子不是服务器的情况。

减去d(v,2)后,即可保证所有儿子不是服务器的情况中,去除了v也不是服务器的情况,此时d(u,1)-d(v,2)=sum(d(v1,2)) 相等了。

代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3
const int N=10005;
const int mod=1e9+7;

int n,t;
int d[N][3],p[N];
vector<int> g[N],vertices;

void dfs(int u,int fa){
    vertices.push_back(u);
    p[u]=fa;
    for (int i=0; i<g[u].size(); i++) {
        int v=g[u][i];
        if (v!=fa) {
            dfs(v, u);
        }
    }
}

int main() {
    while (cin>>n&&n!=-1) {
        int t1,t2;
        for (int i=0; i<n; i++) {
            g[i].clear();
        }
        for (int i=1; i<n; i++) {
            scanf("%d %d",&t1,&t2);
            t1--,t2--;
            g[t2].push_back(t1);
            g[t1].push_back(t2);
        }
        vertices.clear();
        dfs(0, -1);
        for (int i=vertices.size()-1; i>=0; i--) {
            int u=vertices[i];
            d[u][0]=1;
            d[u][1]=0;
            for (int j=0; j<g[u].size(); j++) {
                int v=g[u][j];
                if (v==p[u]) continue;
                d[u][0]+=min(d[v][0],d[v][1]);
                d[u][1]+=d[v][2];
                if (d[u][0]>INF) d[u][0]=INF;
                if (d[u][1]>INF) d[u][1]=INF;
            }
            d[u][2]=INF;
            for (int j=0; j<g[u].size(); j++) {
                int v=g[u][j];
                if (v==p[u]) {
                    continue;
                }
                d[u][2]=min(d[u][2], d[u][1]-d[v][2]+d[v][0]);
            }
        }
        printf("%d\n",min(d[0][0], d[0][2]));
        cin>>n;
    }
    return 0;
}

你可能感兴趣的:(UVa-1228 Perfect Service)