Meaning
An N-node tree that, when removed from a node, becomes a forest.
Each tree in this forest has a number of nodes, where the maximum number of nodes is set to Max.
Ask how much is max when you delete a node?
Ideas
Exactly the same as poj-3107 Godfather! Don't want to spit the groove ...
In fact, did not want to send this article, but found today is the last day of August, but also one of the 80 published. So.. It's a wicked water.
Code
/**===================================================== * is a solution for ACM/ICPC problem * * @source: POJ -1655 Balancing Act * @description: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuangde
@gmail. com * Copyright (C) 2013/08/31 15:32 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 = 20010;
int TOT[MAXN];
int F[MAXN], minx, id; 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 T
Ot[u] + = DFS (v, u);
//Save 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]);} if (Ans & Lt
Minx) {minx = ans; id = u;} else if (ans = = Minx) {id = min (id, u);} return tot[u];
int main () {int ncase; scanf ("%d", &ncase);
while (ncase--) {scanf ("%d", &n);
Initadj ();
for (int i = 0; i < n-1 ++i) {int u, v scanf ("%d%d", &u, &v); Addedge (U, v); Addedge (v, u);} minx = INF;
DFS (1,-1);
printf ("%d%d\n", ID, Minx);
return 0; }