Algorithm: POJ 1947 rebuilding Roads (tree-type backpack DP)

Source: Internet
Author: User

Meaning

To a tree, ask to delete at least a few edges. So that the number of nodes in the remaining subtree is P

Ideas

I read this question a few days ago, but I have no idea, and I think about it every day, until today, I have doubts about my method.

The case, unexpectedly AC.

F (I, j) represents the subtree I, preserving the minimum number of deletions of J nodes, note that here is a subtree of the J node, which refers to the root node

A subtree of I with J nodes. In this way, the state shift is much easier to think about.

For the subtree I, if only 1 nodes are preserved, then the edges of all the son nodes that are connected to it are deleted.

So you can initialize the number of Sons of F (i, 1) = node I

F (i, j), that is, the subtree I preserves J nodes, so for each subtree of I, you can choose to keep the 1,2,.. J-1 a node

So each subtree can be viewed as a set of objects, grouping backpacks for all subtrees

If the subtree V chooses to retain K points, then the subtree I will retain j-k points.

Therefore by the state shifts:

F (i, j) = max{max{f (i,j-k) + f (V, K)-1 | 1<=k<s} | V is the son of I node}

Final ans = min{f (i, p) | 1<=i<=n}

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: POJ -1947 Rebuilding roads * @description: Tree-Type Backpack DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshu
angde@gmail.com * Copyright (C) 2013/08/19 14:30 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
    
> #define MP make_pair using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
    
const int INF = 0X3F3F3F3F;
const int MAXN = 160;
vector<int>adj[maxn];
int n, p;
int F[MAXN][MAXN], TOT[MAXN];
    
int ans; int dp (int u) {//COMPUTE subtree has several nodes tot[u] = 1; for (int i = 0; i < adj[u].size (); ++i) {int v = adj[u][i]; Tot[u] = DP
(v);
}//init f[u][1] = adj[u].size (); DP//http://www.bianceng.cn for (inti = 0; I < adj[u].size ();
++i) {int v = adj[u][i]; for (int s = tot[u], s >= 1;--s) {for (int j = 1; J < s && J <= Tot[v]; ++j)
F[u][s] = min (F[u][s], f[u][s-j] + f[v][j]-1);
} if (Tot[u] >= p) ans = min (ans, f[u][p] + (u!=1));
return Tot[u];
    
int main () {for (int i = 0; I <=n; ++i) adj[i].clear ();
scanf ("%d%d", &n, &p);
    
for (int i = 0; i < n-1 ++i) {int u, v; scanf ("%d%d", &u, &v); Adj[u].push_back (v);}
Memset (f, INF, sizeof (f));
ans = INF;
    
DP (1);
printf ("%d\n", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.