hihoCoder 1055 刷油漆

来源:互联网 发布:明道办公软件登录 编辑:程序博客网 时间:2024/04/28 13:40



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{    int next,to;}tree[110];int n,m;int dp[110][110];int head[110];void dfs(int x){    int p = head[x];    int i,j;    while(p){        dfs(tree[p].to);        p = tree[p].next;    }    p = head[x];    while(p){        for(j = m; j > 1; j--){            for(i = 1; i < j; i++)                dp[x][j] = max(dp[x][j], dp[x][j-i] + dp[tree[p].to][i]);        }        p = tree[p].next;    }}int main(){    while(scanf("%d%d",&n,&m) != EOF){        memset(dp,0,sizeof(dp));        memset(head,0,sizeof(head));        int from,to;        for(int i = 1; i <= n; i++)            scanf("%d",&dp[i][1]);        for(int i = 1; i < n; i++){            scanf("%d%d",&from,&to);            tree[i].to = to;            tree[i].next = head[from];            head[from] = i;        }        dfs(1);        printf("%d\n",dp[1][m]);    }    return 0;}


0 0