HDU 2499 - Rotate to root (Tree DP)
http://www.acmerblog.com/hdu-2499-rotate-to-root-4037.html
Problem Description
Rotate-to-root is a heuristic for balancing binary search trees. In this problem, the contents of the tree will be ignored, and you will be asked about the effect the heuristic has on the structure of the tree.
A binary tree is either empty or consists of a node with a left child and a right child, where each of the children are also binary trees. No node has more than one parent, and there are no cycles. In any non-empty tree, there is exactly one node without a parent, which is called the root of the tree.
The rotate-to-root heuristic takes effect when a node in the tree, say X, is accessed. While X is not the root of the tree, the following procedure is executed:
Tree rotations preserve the order of the nodes in the tree, which is why they are useful, but that is not important for this problem.
The height of a binary tree is the longest length of a path from its root to the bottom of the tree. More formally, the height of the empty tree is 0, and the height of a non-empty tree that has a root node X with children A and B is 1 + max{height(A), height(B)}.
Given a binary tree, you must determine, for each node X, what the height of the tree would be after X is rotated to root.
A binary tree is either empty or consists of a node with a left child and a right child, where each of the children are also binary trees. No node has more than one parent, and there are no cycles. In any non-empty tree, there is exactly one node without a parent, which is called the root of the tree.
The rotate-to-root heuristic takes effect when a node in the tree, say X, is accessed. While X is not the root of the tree, the following procedure is executed:
- If X is the left child of its parent, a right tree rotation is performed. Let P be the parent of X, let A be X's left child, let B be X's right child, and let C be the right child of P. Then, P is replaced in the tree by X, so P's parent (if any) becomes X's parent; X's right child becomes P's left child (which replaces X); and P replaces X's right child. Diagram:
- If X is the right child of its parent, a left tree rotation is performed. Let P be the parent of X, let A be the left child of P, let be B be X's left child, and let C be X's right child. Then, P is replaced in the tree by X, so P's parent (if any) becomes X's parent; X's left child becomes P's right child (which replaces X); and P replaces X's left child. Diagram:
Tree rotations preserve the order of the nodes in the tree, which is why they are useful, but that is not important for this problem.
The height of a binary tree is the longest length of a path from its root to the bottom of the tree. More formally, the height of the empty tree is 0, and the height of a non-empty tree that has a root node X with children A and B is 1 + max{height(A), height(B)}.
Given a binary tree, you must determine, for each node X, what the height of the tree would be after X is rotated to root.
Input
Input consists of a number of test cases. The first line of each test case contains the integer N, the number of nodes in the binary tree, where 1 <= N <= 105.
The following N lines of input each contain two integers. The ith pair of these integers is li and ri, the left and right children of node i. If li = 0, then the left child of node i is the empty tree, and if ri = 0, then the right child of node i is the empty tree. Otherwise, 1 <= l, r <= N.
It is guaranteed that the input will describe a binary tree.
The last test case is followed by a line containing the integer 0. This final line is not a test case and should not be processed.
The following N lines of input each contain two integers. The ith pair of these integers is li and ri, the left and right children of node i. If li = 0, then the left child of node i is the empty tree, and if ri = 0, then the right child of node i is the empty tree. Otherwise, 1 <= l, r <= N.
It is guaranteed that the input will describe a binary tree.
The last test case is followed by a line containing the integer 0. This final line is not a test case and should not be processed.
Output
Output consists of N lines for each test case, where the ith line contains an integer giving the height of the tree after node i is rotated to root.
Sample Input
4 2 3 4 0 0 0 0 0 0
Sample Output
3 3 4 3
07 | const int N = 100010; |
08 |
09 | int n, root, f[N], l[N], r[N], d[N], w[N]; |
10 |
11 | int max( int x, int y) |
12 | { |
13 | return x > y ? x : y; |
14 | } |
15 |
16 | void init( int u) |
17 | { |
18 | if (u == 0) return ; |
19 | init(l[u]); |
20 | init(r[u]); |
21 | d[u] = max(d[l[u]], d[r[u]])+1; |
22 | } |
23 |
24 | void dp( int x, int <, int &rt) |
25 | { |
26 | int p; |
27 | p = f[x]; |
28 | if (x == root) return ; |
29 | if (l[p] == x) |
30 | { |
31 | w[p] = max(lt+1, max(rt, d[r[p]])+2); |
32 | rt = max(rt, d[r[p]])+1; |
33 | } |
34 | else |
35 | { |
36 | w[p] = max(rt+1, max(d[l[p]], lt)+2); |
37 | lt = max(d[l[p]], lt)+1; |
38 | } |
39 | dp(p, lt, rt); |
40 | } |
41 |
42 | int main() |
43 | { |
44 | int i, a, b; |
45 | while ( scanf ( "%d" , &n) != EOF) |
46 | { |
47 | if (n == 0) break ; |
48 | for (i = 1; i <= n; i++) f[i] = i; |
49 | for (i = 1; i <= n; i++) |
50 | { |
51 | scanf ( "%d%d" , &a, &b); |
52 | f[a] = f[b] = i; |
53 | l[i] = a; |
54 | r[i] = b; |
55 | } |
56 | root = 1; |
57 | while (f[root] != root) root = f[root]; |
58 | d[0] = 0; |
59 | init(root); |
60 | for (i = 1; i <= n; i++) |
61 | { |
62 | w[0] = 0; |
63 | a = d[l[i]]; |
64 | b = d[r[i]]; |
65 | if (i == root) w[root] = max(d[l[root]], d[r[root]])+1; |
66 | else dp(i, a, b); |
67 | printf ( "%d\n" , w[root]); |
68 | } |
69 | } |
70 | return 0; |
71 | } |