UVA 1218 Perfect Services (树形DP经典)*

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

#include<bits/stdc++.h>
#define maxn 10005
#define INF 100000
using namespace std;
int n,x,y;
int dp[maxn][3];///0代表i是服务器,1代表i不是服务器但父节点是服务器,2代表i不是服务器但父节点不是服务器
vector<int> sons[maxn];
/*
题目大意:给定一个树形结构,求支配集的最小数量。

对于一个节点,如果该节点是服务器,
那么状态可以往下推,子节点可以是服务器也可以不是。
如果该节点不是服务器,那么状态稍微复杂点,
因为还要考虑其父节点是不是服务器,
如果父节点是服务器,那么可以肯定子节点均不是服务器。
如果父节点也不是服务器,那么子节点中只有一个是服务器。
这种状态也需要思考下,因为直接枚举太烦了,
可以用之前计算好的状态来构造状态。
即容斥原理。

*/
void Dp(int u,int pre)
{
    dp[u][0]=1;
    dp[u][1]=0;
    dp[u][2]=INF;
    int k=sons[u].size();

    for(int i=0;i<k;i++)
    {
        int v=sons[u][i];
        if(v==pre) continue;
        Dp(v,u);
        dp[u][0]+=min(dp[v][0],dp[v][1]);
        dp[u][1]+=dp[v][2];
    }
    for(int i=0;i<k;i++)
    {
        int v=sons[u][i];
        if(v==pre) continue;
        dp[u][2]=min(dp[u][2],dp[u][1]-dp[v][2]+dp[v][0]);
    }
    if(dp[u][0]>INF) dp[u][0]=INF;
    if(dp[u][1]>INF) dp[u][1]=INF;
    if(dp[u][2]>INF) dp[u][2]=INF;
}

int main()
{
     ///#ifdef LOCAL
    ///      freopen("input.txt","r",stdin);
    ///#endif
    int flag=0;
    while(scanf("%d",&n))
    {
        memset(dp,0xff,sizeof(dp));
        for(int i=1;i<=n;i++) sons[i].clear();

        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            sons[x].push_back(y);
            sons[y].push_back(x);
        }

         int ans=INF;
         Dp(1,-1);///其实记忆化搜索都不是必要的。
         ///因为是树形,考虑
         ans=min(ans, dp[1][0] );
         ans=min(ans, dp[1][2] );
         printf("%d\n",ans);

         scanf("%d",&flag);
         if(flag==-1) break;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81323567