【URAL1018】Binary Apple Tree
阿新 • • 發佈:2020-08-11
Binary Apple Tree
題目描述
Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enumerate by integers the root of binary apple tree, points of branching and the ends of twigs. This way we may distinguish different branches by their ending points. We will assume that root of tree always is numbered by \(1\)
\ /
3 4
\ /
1
5 2 1 3 1 1 4 10 2 3 20 3 5 20
21## 題解 題意:有一棵樹,每條邊都有一個權值,求包含根的有$Q$條邊的聯通塊的權值之和最大。 那麼我們考慮樹上dp $dp[i][j]$表示以$i$為根,保留$j$條邊最大的權值之和。 那麼轉移方程如下: $dp[u][i]=max$($dp[u][i],dp[u][i-k-1]+dp[son][k]$) $dp[u][0]=val[u][fa]$ 記得把$dp[u][0]$的值初始化為$u$和它父親的邊的權值,如果$u$不是根結點。 上程式碼: ```cpp #include