Tim Au Yeung
Dynamic Programming on Tree
“always” from leaves to root
Children node pass information to parent,
obtain solution from root
Unrooted tree
Choose any node to be the root
Rooted tree:
Use specific root
Build tree
DFS/BFS
Set base case on leaves
Backtrack and update the optimal value for
each node
Obtain solution at root
http://poj.org/problem?id=1655
Given a tree with N nodes
Define Balance[i] as the max size of subtrees
after removing node i
Output: x
where Balance[x] is min
PreProcess
DFS and DP: get num[i], the number of nodes in
subtree rooted at i
Calculate Balance[i]
http://poj.org/problem?id=2486
Given undirected tree with N nodes (1..N)
Wi for node i (1<=i<=N)
Gain Wi score when you FIRST visit node i
Start from node 1
At most K step
Output: Max score
N<=100; K<=200;
DFS from node 1
dp[i][j][0]:
max score obtained in subtree rooted at node i
starting at node i
walk at most j step
back to node i
dp[i][j][1]:
max score obtained in subtree rooted at node i
starting at node i
walk at most j step
May NOT back to node i
Ans = dp[1][K][1]
http://poj.org/problem?id=1947
Given unrooted tree with N nodes
Output min number of edges whose
destruction would isolate a subtree with
exactly P nodes
1 <= N <= 150; 1 <= P <= N;
dp[i][j]: min number of edges destruction to
isolate a subtree rooted at i and with j nodes
Consider child x
If reserve x,
Else
dp[i][j] = min(dp[i][j-k]+dp[x][k]) 0 <= k <= j
dp[i][j] = dp[i][j] + 1
Ans: min{dp[i][P]}
http://codeforces.com/problemset/problem/132/D
You are given an integer n. You have to
represent it as n = a1 + a2 + ... + am, where each
of ai is a non-negative power of 2, possibly
multiplied by -1.
Find a representation which minimizes the
value of m.
Input: positive integer n, written as its binary
notation. The length of the notation is at most
106.
http://codeforces.com/problemset/problem/95/E
Positive integers are lucky if their it doesn't contain
digits other than 4 and 7.
Each island belongs to exactly one region, there is a
path between any two islands located in the same
region; there is no path between any two islands
from different regions.
A region is lucky if the amount of islands in it is a
lucky number.
Find the minimum number of roads needed to build
to create a lucky region.
n: number of islands; m: the number of roads
1 ≤ n, m ≤ 105
http://codeforces.com/problemset/problem/176/D
A Hyper String is made by concatenation of
some base strings. Suppose you are given a list
of base strings b1, b2, ..., bn. Now the Hyper
String made from indices list i1, i2, ..., im is
concatenation of base strings bi1, bi2, ..., bim.
Compute the length of the longest common
sub-sequence of a Hyper String t with a string s
1 ≤ n ≤ 2000
1 ≤ m ≤ 2000
Tree DP
NOI 2003 逃学的小孩
NOI 2002 贪吃的九头龙
Ural 1031 1039 1056 1073 1078
POJ 1947 1155 1655 3107 2486
http://codeforces.com/problemset/tags/dp