1. 程式人生 > >Perfect Service (最小支配集)

Perfect Service (最小支配集)

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 toN. 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 nextN − 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 13 23 34 45 46 0
2 12 -1

Sample Output

2 1

題意:在一棵無根樹上找最少的點建立伺服器,使得其他點都有且與一個伺服器相連。

題解:樹形dp,dp[i][0]和dp[i][2]均表示以i為根的最小支配集,dp[i][0]表示i是伺服器,則每個子節點可以是伺服器,也可以不是伺服器,dp[i][1]表示i的父親是伺服器i不是伺服器,則i的子節點一定不是伺服器,dp[i][2]表示i和i的父親都不是伺服器,則i恰有一個子節點是伺服器。

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e4 + 7;
const int maxn =  1e4+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

vector<int>ve[maxn];
int dp[maxn][3]; //dp[i][0]表示i是伺服器,則每個子節點可以是伺服器,也可以不是伺服器
                 //dp[i][1]表示i的父親是伺服器i不是伺服器,則i的子節點一定不是伺服器
                 //dp[i][2]表示i存在一個子節點是伺服器,則i和i的父親都不是伺服器
int dfs(int u, int fa){
    ULL sz = ve[u].size();
    for(int i=0; i<sz; i++) {
        int v = ve[u][i];
        if(v == fa) continue;
        dfs(v ,u);
        dp[u][0] += min(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][2];
    }
    for(int i=0; i<sz; i++){
        int v = ve[u][i];
        if(v == fa) continue;
        dp[u][2] = min(dp[u][2], dp[u][1] - dp[v][2] + dp[v][0]);
    }
    return min(dp[u][0], dp[u][2]);
}

int main(){
    int n ;
    while(~scanf("%d", &n)){
        MT(dp, 0);
        for(int i=1; i<=n; i++){
            ve[i].clear();
            dp[i][0] = 1;
            dp[i][1] = 0;
            dp[i][2] = 10001;
        }
        for(int i=1; i<n; i++) {
            int u, v; scanf("%d%d", &u, &v);
            ve[u].push_back(v);
            ve[v].push_back(u);
        }
        
        int ans = dfs(1, -1);
        printf("%d\n", ans);
        
        int p; scanf("%d", &p);
        if(p == -1) break;
    }
    return 0;
}