[UVa1218/POJ3398] Perfect Service (树形DP)

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

 

题意分析

输入给定一个无根树,要求选树上的最少的节点,使得每个没被选节点恰好与一个被选的节点相邻。

本题可以采用树形dp来解决。

参考《算法竞赛入门经典》,我们按照每个结顶的情况进行分类。

  1. dp[u, 0]: 表示 u 是服务器,则每个子节点可以是服务器或者不是服务器;
  2. dp[u, 1]: 表示 u 不是服务器,但 u 的父亲是服务器,这意味着 u 的所有子结点都不是服务器;
  3. dp[u, 2]: 表示 u 不是服务器,u 的父亲也不是服务器,这意味着 u 恰好有一个子结点是服务器

则状态转移方程为:

  • dp[u][0] = sum{ min(dp[v][0], dp[v][1]) } + 1
  • dp[u][1] = sum{ dp[v][2] }

       dp[u][2] = min(​​​​​​​dp[u][2], sum{ dp[v][2] } - dp[v][2] + dp[v][0])     //利用第二个状态转移方程,则课化简为下面的方程

  • dp[u][2] = min(dp[u][2], -dp[v][2] + dp[v][0]) + dp[u][1]

 

AC代码

#include <iostream>
#include <cstdio>
#include <vector>
#define INF 10000000
using namespace std;


int N, dp[10005][4];
vector<int> G[10005];

void Tdp(int u, int fa)
{
    dp[u][0] = 1;
    dp[u][1] = 0;
    dp[u][2] = INF;
    for(int i = 0; i < G[u].size(); i++){  //枚举儿子
        int v = G[u][i];
        if(v == fa)  continue;  //跳过父亲结点 
        Tdp(v, u);  //递归
        dp[u][0] += min(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][2];
        dp[u][2] = min(dp[u][2], -dp[v][2] + dp[v][0]);
    }
    dp[u][2] += dp[u][1];
}

void solve()
{
  
  for(int i = 1; i <= N; i++)  G[i].clear();
  int a, b;
  for(int i = 1; i < N; i++)
  {
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  Tdp(1, -1); //没父节点,记为-1 
  cout << min(dp[1][0], dp[1][2]) << endl;
}

int main()
{
  while(scanf("%d", &N) != EOF)
  {
    solve();
    scanf("%d", &N);
    if(N == -1)  break;
  }
  return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值