Training camp Day 8 2020.3.8 Dynamic Programming (c)

Training camp Day 8 2020.3.8

Dynamic Programming (c)

1.P1352 no party boss

A university there \ (N \) staff members, numbered \ (1 \) ~ \ (N \) . There are dependencies between them, that is their direct supervisor relationship is like a tree rooted to the principal, the parent node is the child node. Now there is the anniversary banquet, invited to a banquet every employee will increase a certain happiness index \ (R_i \) , but then, if the direct supervisor of a staff member to attend the prom, then the staff would in any case would not come to the ball. Therefore, you programmed computing, which allows staff to invite the greatest happiness index, find the greatest happiness index.

answer

This probably is the classic tree dp it ......
f [x] [0/1] to x is the root of sub-tree, and do not participate in the x / dance happy maximum value of
the

f[x][0]=sigma{max(f[y][0],f[y][1])} (y是x的儿子)
f[x][1]=sigma{f[y][0]}+happy[x] (y是x的儿子)

First find the root roots only
the \ (\ text {ans} = \ max (f_ {root, 0}, f_ {root, 1}) \)

2.P2016 strategy game

Now the soldiers are to be arranged in a tree, each soldier on the nodes, each side of its soldiers to guard all the nodes directly connected to at least ask how many soldiers arrangement.

answer

dp [i] [0] indicates that the point is not selected (so all child nodes must be selected from a), it is selected from the son +
dp [i] [1] indicates that the click, then the child node is + min ( son selected / not selected)
the answer is min (dp [root] [0 ], do [root] [1]);

3.POJ2378

To a tree, ask what you can delete nodes so that the number of nodes in each sub-graph of the rest of the \ (\ le \) / total number of 2.

answer

All subtree son's obviously a node is \ (\ le \) / total number of 2 n- and its sub-tree node \ (\ le \) / total number of 2
then this point is a valid point.
Then the size [i] can represent the number of sub-tree node i node, usually we call sub-tree size (so this is not a problem dp ah)

4.ZOJ-3201

To a tree, a little right, ask the tree the size of the largest sub-tree and k is the number of weights

answer

Set DP [i] [j] i represents the point size of the maximum weight j of the subtree and then

dp[u][j] = max(dp[u][j], dp[u][k] + dp[v][j - k])

Complexity \ (O (KN ^ 2) \) , but it is not optimal complexity.
Later, we will be further optimized.

5.POJ-3659

For \ (n \ in [1,10000] \) points, \ (. 1-n \) sides, tree, taking as little as possible from the point n points constituting a set, so that all the remaining points are point part can be connected to this collection.

answer

And soldiers that question is not very similar?
This question dp but also need to consider the impact of his father's son
dp [i] [0] represents the i node is not arranged soldier, i father going to cover i.
dp [i] [1] i represents the node arrangement not soldiers, but the presence of its child nodes are arranged soldiers nodes (it has been covered i)
DP [i] [2] i represents the node arrangement soldier

It is dp [u] [1] more intractable?

And at least one selected from the group dp [v] [2], if it is naturally chose the best.
Otherwise dp [u] [1] + = min (dp [v] [2] -dp [v] [1])

operation

1.HDU-2196

To a tree, there are n nodes, the value of the right side between nodes, each node Q furthest node from its far.
Tip:
https://blog.csdn.net/u011426016/article/d etails / 89,164,896

2.P1077 placed flowers

P1352, P2016,2378 WOMEN @ 3201 @ As such, 3659 @ ​​WOMEN, 2196 @ HDU, P1077

Guess you like

Origin www.cnblogs.com/liuziwen0224/p/xjx8.html