码迷,mamicode.com
首页 > 其他好文 > 详细

A Knapsack Problem - hackerrank

时间:2018-11-09 23:00:17      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:problem   题解   space   nap   line   btree   sub   int   oid   

题意

\(n(1 \leq n \leq 1000)\)个结点的树,每个结点有重量\(w\)和价值\(v\)两个属性,现在给你一个大小为\(m(1 \leq m \leq 1000)\)的背包,求能得到的最大价值。注:选取的结点一定是一个连通块。

题解

待补

代码

using namespace std;

const int N = 2005;

int n, m;
int s[N], v[N], subtree[N], label[N], dp[N][N];
bool deleted[N];

vector<int> adj[N];

void DFS(int u, int par = -1) {
    subtree[u] = 1;
    for (auto v : adj[u]) if ((!deleted[v]) && (v != par)) {
        DFS(v, u);
        subtree[u] += subtree[v];
    }
    label[n] = u; n--;
}

int central(int u, int par = -1) {
    int w = 0;
    for (auto v : adj[u]) if ((!deleted[v]) && (v != par)) {
        if (subtree[v] > subtree[w]) w = v;
    }
    if (w == 0) return (u);
    if (subtree[w] + subtree[w] <= n) return (u);
    return (central(w, u));
}

int calc(int n) {
    Rep(i, 0, n) Rep(j, 0, m) dp[i][j] = -INF32;

    dp[1][s[label[1]]] = v[label[1]];

    Rep(i, 2, n) {
        int u = label[i];
        Rep(j, 0, m) {
            if (j >= s[u]) dp[i][j] = max(dp[i][j], dp[i - 1][j - s[u]] + v[u]);
            dp[i + subtree[u] - 1][j] = max(dp[i + subtree[u] - 1][j], dp[i - 1][j]);
        }
    }

    int res = 0;
    Rep(i, 0, m) res = max(res, dp[n][i]);
    return res;
}

int solve(int tree, int tree_size) {
    n = tree_size; DFS(tree);
    n = tree_size;

    int r = central(tree);
    n = tree_size; DFS(r);

    int res = calc(tree_size);
    deleted[r] = true;

    for (auto v : adj[r]) if (!deleted[v]) res = max(res, solve(v, subtree[v]));
    return res;
}

int main()
{
    BEGIN() {
        sc(n), sc(m);
        Rep(i, 0, n) deleted[i] = false;
        Rep(i, 0, n) adj[i].clear();

        Rep(i, 1, n) sc(s[i]);
        Rep(i, 1, n) sc(v[i]);
        rep(i, 1, n) {
            int u, v;
            sc(u), sc(v);
            adj[u].pb(v);
            adj[v].pb(u);
        }
        int result = solve(1, n);
        pr(result);
    }
    return 0;
}

A Knapsack Problem - hackerrank

标签:problem   题解   space   nap   line   btree   sub   int   oid   

原文地址:https://www.cnblogs.com/zgglj-com/p/9937494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!