Poj 3107 Godfather (tree dp)

Source: Internet
Author: User

Poj-3107 to give a tree with n nodes, the node number is 1 ~ N. After you delete a node, try to minimize the number of nodes in the remaining branches. There may be multiple solutions, which are output by serial number. Simple tree-like dp. In fact, even dp cannot be counted... it is to calculate the number of nodes in each subtree by directly counting statistics first by dfs. tot [I]. Update dfs again. Answer: f [I] = max (n-tot [I], max {tot [v] | v is the son of I }); the two dfs can be combined in one dfs to complete the complex O (n) code.

/** =================================================== =================== * This is a solution for ACM/ICPC problem ** @ source: poj-3107 Godfather * @ description: Tree dp * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * Copyright (C) 2013/08/30 All rights reserved. * ===================================================== ===================*/# include <iostream> # include <cstdio> # include <Algorithm> # include <vector> # include <cstring> using namespace std; typedef pair <int, int> PII; typedef long int64; const int INF = 0x3f3f3f3f; const int MAXN = 50010; int tot [MAXN]; int f [MAXN], minx; namespace Adj {int size, head [MAXN]; struct Node {int v, next ;} E [MAXN * 2]; inline void initAdj () {size = 0; memset (head,-1, sizeof (head);} inline void addEdge (int u, int v) {E [size]. v = v; E [size ]. Next = head [u]; head [u] = size ++ ;}} using namespace Adj; int n; int dfs (int u, int fa) {tot [u] = 1; // count for (int e = head [u]; e! =-1; e = E [e]. next) {int v = E [e]. v; if (v = fa) continue; tot [u] + = dfs (v, u );} // calculate the answer int & ans = f [u] = n-tot [u]; for (int e = head [u]; e! =-1; e = E [e]. next) {int v = E [e]. v; if (v = fa) continue; ans = max (ans, tot [v]);} minx = min (minx, ans); return tot [u];} int main () {while (~ Scanf ("% d", & n) {initAdj (); for (int I = 0; I <n-1; ++ I) {int u, v; scanf ("% d", & u, & v); addEdge (u, v); addEdge (v, u) ;} minx = INF; dfs (1, -1); bool first = true; for (int I = 1; I <= n; ++ I) if (f [I] = minx) {if (first) first = false, printf ("% d", I); else printf ("% d", I) ;}puts ("") ;}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.