Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tim Au Yeung.  Dynamic Programming on Tree  “always” from leaves to root  Children node pass information to parent, obtain solution from root  Unrooted.

Similar presentations


Presentation on theme: "Tim Au Yeung.  Dynamic Programming on Tree  “always” from leaves to root  Children node pass information to parent, obtain solution from root  Unrooted."— Presentation transcript:

1 Tim Au Yeung

2  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

3  Build tree  DFS/BFS  Set base case on leaves  Backtrack and update the optimal value for each node  Obtain solution at root

4  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

5  PreProcess  DFS and DP: get num[i], the number of nodes in subtree rooted at i  Calculate Balance[i]

6  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;

7  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]

8  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;

9  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,  dp[i][j] = min(dp[i][j-k]+dp[x][k]) 0 <= k <= j  Else  dp[i][j] = dp[i][j] + 1  Ans: min{dp[i][P]}

10  http://codeforces.com/problemset/problem/132/D  You are given an integer n. You have to represent it as n = a 1 + a 2 +... + a m, where each of a i 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 10 6.

11  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 ≤ 10 5

12  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 b 1, b 2,..., b n. Now the Hyper String made from indices list i 1, i 2,..., i m is concatenation of base strings b i1, b i2,..., b im.  Compute the length of the longest common sub-sequence of a Hyper String t with a string s  1 ≤ n ≤ 2000  1 ≤ m ≤ 2000

13  Tree DP  NOI 2003 逃学的小孩  NOI 2002 贪吃的九头龙  Ural 1031 1039 1056 1073 1078  POJ 1947 1155 1655 3107 2486  http://codeforces.com/problemset/tags/dp


Download ppt "Tim Au Yeung.  Dynamic Programming on Tree  “always” from leaves to root  Children node pass information to parent, obtain solution from root  Unrooted."

Similar presentations


Ads by Google