tim au yeung. dynamic programming on tree “always” from leaves to root children node pass...

13
Tim Au Yeung

Upload: neil-allen-porter

Post on 21-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

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

Tim Au Yeung

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

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

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

Build tree DFS/BFS Set base case on leaves Backtrack and update the optimal value

for each node Obtain solution at root

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

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

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

PreProcess DFS and DP: get num[i], the number of nodes

in subtree rooted at i Calculate Balance[i]

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

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;

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

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]

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

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;

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

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

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

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.

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

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

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

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

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

Tree DP NOI 2003 逃学的小孩 NOI 2002 贪吃的九头龙 Ural 1031 1039 1056 1073 1078 POJ 1947 1155 1655 3107 2486

http://codeforces.com/problemset/tags/dp