1 / 13

Dynamic Programming (II)

Dynamic Programming (II). Tim Au Yeung. Tree DP. 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. Tree DP. Build tree DFS/BFS

lwhitmire
Download Presentation

Dynamic Programming (II)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Programming (II) Tim Au Yeung

  2. Tree DP • 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. Tree DP • Build tree • DFS/BFS • Set base case on leaves • Backtrack and update the optimal value for each node • Obtain solution at root

  4. Tree DP - Balancing Act • 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. Tree DP - Balancing Act • PreProcess • DFS and DP: get num[i], the number of nodes in subtree rooted at i • Calculate Balance[i]

  6. Tree DP - Apple Tree • 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. Tree DP - Apple Tree • 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. Tree DP - Rebuilding Roads • 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. Tree DP - Rebuilding Roads • 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. Advanced DP - Constants in the language of Shakespeare • 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.

  11. Advanced DP - Lucky Country • 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

  12. Advanced DP - Hyper String • 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

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

More Related