Poj1947 rebuilding roads

Source: Internet
Author: User
Rebuilding roads
Time limit:1000 ms   Memory limit:30000 K
Total submissions:9105   Accepted:4122

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 cocould do. he wants to know the minimum number of roads whose destruction wowould 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 61 21 31 41 52 62 72 84 94 104 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.] question: The minimum number of cut times to cut the subtree of a p node. Do not think it is to cut P points... it is the minimum cut number required for splitting the 1 DP [I] [J] node I into the J subtree. There are two types of transfer. The first method is to cut the subtree, which requires + 1, the second method is to merge the subtree to view the code. Be careful not to update each other for five times. The error is: 1 submit randomly, 2 update each other, and 3 forget to switch the non-root subtree.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 152; const int INF = 0x7ffff; int DP [maxn] [maxn]; int des [maxn]; // The intermediate cache prevents update of int e [maxn] [maxn]; int Len [maxn]; // create an int lef [maxn]; // subnode + number of itself int N, P; void DFS (INT s) {lef [s] = 1; // certainly, the child node has not added DP [s] [1] = 0; // This time is only impossible if (LEN [s] = 0) {return ;} // No need to deliberately for (INT I = 0; I <Len [s]; I ++) {int T = E [s] [I]; DFS (t ); fill (DES, des + n + 1, INF); // initialize the cache for (int K = 1; k <= lef [s]; k ++) {des [k] = DP [s] [k] + 1; // cut} For (int K = 1; k <= lef [s]; k ++) {for (Int J = 1; j <= lef [T]; j ++) {des [K + J] = min (DP [s] [k] + dp [T] [J], des [K + J]); // not feasible} lef [s] + = lef [T]; // Add this for (int K = 1; k <= lef [s]; k ++) {DP [s] [k] = des [k]; // retrieves the status from the cache} DP [s] [lef [s] = 0; // not required} int main () {scanf ("% d", & N, & P); memset (Len, 0, sizeof (LEN )); for (INT I = 1; I <= N; I ++) Fill (DP [I] + 1, DP [I] + n + 1, INF ); for (INT I = 2; I <= N; I ++) {int F, T; scanf ("% d", & F, & T ); E [f] [Len [f] ++] = T;} DFS (1); int ans = DP [1] [p]; // 1 is the root node separation. It does not need to be switched for (INT I = 2; I <= N; I ++) ans = min (ANS, DP [I] [p] + 1); // cut all non-root subtree // printdp (); printf ("% d \ n", ANS ); return 0 ;}

  

Poj1947 rebuilding roads

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.