POJ1947---Rebuilding Roads(树形dp,背包)

本文探讨了一种方法,在特定树状农场结构中,通过最小化破坏的道路数量来隔离特定数量的农场。利用动态规划算法,计算出在保持一定数量农场联通的情况下,至少需要破坏多少道路。实例分析了当目标为隔离6个农场时的具体步骤和结果。

Description
The cows have reconstructed Farmer John’s farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn’t have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.

Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.

Input
* Line 1: Two integers, N and P

  • Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J’s parent in the tree of roads.

Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.

Sample Input

11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11

Sample Output

2

Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]

Source

dp[u][size] 表示以u为根的子树,保留了size个节点时,最少需要删除的边个数
dp[u][size]=min(dp[u][size],dp[u][sizek]+dp[v][k]1)

最后求个最小值,但是对于不是根的节点,答案要+1(与他的父亲节点脱离)

/*************************************************************************
    > File Name: POJ1947.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月12日 星期二 16时50分33秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int num[155];
int dp[155][155];
vector <int> tree[155];

void DP(int u) {
    num[u] = 1;
    int size = tree[u].size();
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        DP(v);
        num[u] += num[v];
    }
    dp[u][1] = size;
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        for (int j = num[u]; j > 0; --j) {
            for (int k = 1; k <= j && k <= num[v]; ++k) {
                if (dp[v][k] == inf || dp[u][j - k] == inf) {
                    continue;
                }
                dp[u][j] = min(dp[u][j], dp[v][k] + dp[u][j - k] - 1);
            }
        }
    }
}

int main() {
    int n, p;
    while (~scanf("%d%d", &n, &p)) {
        memset(dp, inf, sizeof(dp));
        for (int i = 1; i <= n; ++i) {
            tree[i].clear();
            num[i] = 0;
        }
        int u, v;
        for (int i = 1; i < n; ++i) {
            scanf("%d%d", &u, &v);
            tree[u].push_back(v);
        }
        DP(1);
        int ans = inf;
        for (int i = 1; i <= n; ++i) {
            if (num[i] < p) {
                continue;
            }
            if (i == 1) {
                ans = min(ans, dp[i][p]);
            }
            else {
                ans = min(ans, dp[i][p] + 1);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
物联网通信协议测试是保障各类设备间实现可靠数据交互的核心环节。在众多适用于物联网的通信协议中,MQTT(消息队列遥测传输)以其设计简洁与低能耗的优势,获得了广泛应用。为确保MQTT客户端与服务端的实现严格遵循既定标准,并具备良好的互操作性,实施系统化的测试验证至关重要。 为此,采用TTCN-3(树表结合表示法第3版)这一国际标准化测试语言构建的自动化测试框架被引入。该语言擅长表达复杂的测试逻辑与数据结构,同时保持了代码的清晰度与可维护性。基于此框架开发的MQTT协议一致性验证套件,旨在自动化地检验MQTT实现是否完全符合协议规范,并验证其与Eclipse基金会及欧洲电信标准化协会(ETSI)所发布的相关标准的兼容性。这两个组织在物联网通信领域具有广泛影响力,其标准常被视为行业重要参考。 MQTT协议本身存在多个迭代版本,例如3.1、3.1.1以及功能更为丰富的5.0版。一套完备的测试工具必须能够覆盖对这些不同版本的验证,以确保基于各版本开发的设备与应用均能满足一致的质量与可靠性要求,这对于物联网生态的长期稳定运行具有基础性意义。 本资源包内包含核心测试框架文件、一份概述性介绍文档以及一份附加资源文档。这些材料共同提供了关于测试套件功能、应用方法及可能包含的扩展工具或示例的详细信息,旨在协助用户快速理解并部署该测试解决方案。 综上所述,一个基于TTCN-3的高效自动化测试框架,为执行全面、标准的MQTT协议一致性验证提供了理想的技术路径。通过此类专业测试套件,开发人员能够有效确保其MQTT实现的规范符合性与系统兼容性,从而为构建稳定、安全的物联网通信环境奠定坚实基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值