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;