HDU-1561

 

依赖背包转化为树形dp

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <vector>
 6 #define max(x, y) (x > y ? x : y)
 7 #define min(x, y) (x > y ? y : x)
 8 #define INF 0x3f3f3f3f
 9 #define MOD 1000000007
10 #define Yes printf("Yes\n")
11 #define No printf("No\n")
12 using namespace std;
13 typedef long long LL;
14 typedef pair<int, int> PII;
15 
16 const int maxn = 210;
17 int dp[maxn][maxn];
18 
19 vector<int> G[maxn];
20 int N, M;
21 
22 void dfs(int node, int m) {
23     for (int i = 0; i < G[node].size(); i++) {
24         if (m > 1) dfs(G[node][i], m - 1);
25         for (int j = m + 1; j >= 2; j--) {
26             for (int k = 1; k < j; k++) {
27                 dp[node][j] = max(dp[node][j], dp[node][j - k] + dp[G[node][i]][k]);
28             }
29         }
30     }
31 }
32 
33 int main(int argc, const char * argv[]) {
34     while (scanf("%d%d", &N, &M), N + M) {
35         int u;
36         memset(dp, 0, sizeof(dp));
37         for (int i = 0; i <= N; i++) G[i].clear();
38         for (int i = 1; i <= N; i++) {
39             scanf("%d%d", &u, &dp[i][1]);
40             G[u].push_back(i);
41         }
42         dfs(0, M);
43         printf("%d\n", dp[0][M + 1]);
44     }
45     return 0;
46 }

 

posted @ 2017-07-31 23:00  xFANx  阅读(132)  评论(0编辑  收藏  举报