Question Link
Although the question is not difficult, 1A is still very good, but I just started to understand the wrong question and thought for a long time. In addition, it is said that this question will time out when vector is used. After reading this question, we should use the adjacent string.
Question:
To give a tree, make sure it is a tree. Find the maximum value of the number of nodes in the connected block after removing a vertex, find such a vertex, and output it in ascending order.
Analysis:
D [Father] = max (n-sum, d [son]); sum indicates the total number of all nodes below this node, remove the maximum number of nodes in the connected block of a node, which is equal to the number of nodes in the entire tree minus the total number of nodes and the number of sub-trees.
Maximum value.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # include <vector> 8 # define ll _ int64 9 const int maxn = 50000 + 10; 10 const int INF = 1 <28; 11 using namespace STD; 12 INT head [maxn], vis [maxn], T, d [maxn]; 13 int Mi, N; 14 struct node15 {16 int U, V, Ne; 17} e [2 * maxn]; 18 19 void add (int u, int v) 20 {21 E [T]. U = u; 22 E [T]. V = V; 23 E [T]. ne = head [u]; 24 head [u] = T ++; 25} 26 int DFS (INT son, int FA) 27 {28 int I, TMP, sum = 1, x; // sum is the total number of nodes in the subtree where son is the root node 29 for (I = head [son]; I! =-1; I = E [I]. Ne) 30 {31 TMP = E [I]. V; 32 If (TMP = FA) continue; // avoid going back. 33 X = DFS (TMP, son); 34 sum + = x; 35 d [son] = max (d [son], X ); 36} 37 d [son] = max (d [son], n-sum); 38 If (d [son] <mi) 39 MI = d [son]; 40 return sum; 41} 42 int main () 43 {44 int I, F; 45 while (~ Scanf ("% d", & N) 46 {47 memset (E, 0, sizeof (e); 48 memset (Head,-1, sizeof (head )); 49 memset (VIS, 0, sizeof (VIS); 50 memset (D, 0, sizeof (d); 51 T = 0; 52 MI = inf; 53 54 for (I = 1; I <n; I ++) 55 {56 int U, V; 57 scanf ("% d", & U, & V); 58 add (u, v); 59 add (v, U); 60} 61 DFS (1,-1 ); // regard the given tree as a root node. 62 63 f = 0; 64 for (I = 1; I <= N; I ++) 65 {66 If (d [I] = mi) 67 {68 if (f) 69 printf ("% d", I); 70 else71 printf ("% d", I); 72 f = 1; 73} 74} 75 printf ("\ n"); 76} 77 return 0; 78}
Poj 3107 Godfather (tree DP)