Meaning
to a N-node tree, the node number is 1~n, ask to delete a node, so that the remaining branches in the largest number of nodes as little as possible.
There may be a variety of scenarios, which are output in numbered order.
Ideas
A simple tree DP. In fact, even the DP can not be counted ... is direct counting statistics
First DFS computes the number of nodes per node tree tot[i].
DFS Update answer again:
F[i] = max (N-tot[i], max{tot[v] | V is son of i});
Two Dfs can be combined in a DFS to complete, complexity O (n)
Code
/**===================================================== * is a solution for ACM/ICPC problem * * @source: poj- 3107 Godfather * @description: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuangde@gmai
l.com * Copyright (C) 2013/08/30 16:26 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> #
Include <algorithm> #include <vector> #include <cstring> using namespace std;
typedef pair<int, int >PII;
typedef long 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++;
The using namespace Adj;
int n; int dfs (int u, int fa) {Tot[u] = 1;//Count//http://www.bianceng.cn 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 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 = mi
N (minx, ans);
return Tot[u]; int main () {while (~SCANF ("%d", &n)) {Initadj ()-for (int i = 0; i < n-1; ++i) {int u, v; scan
F ("%d%d", &u, &v);
Addedge (U, v);
Addedge (V, u);
} minx = INF;
DFS (1,-1);
bool A = true; for (int i = 1; I <= n; ++i) if (f[i] = = Minx) {if (a) = False, printf ("%d", I); else printf ("%d", I);} PU
TS ("");}
return 0; }