zoukankan      html  css  js  c++  java
  • LuoguP2015 二叉苹果树 树形dp

    这道题被我秒了是我太强了还是这道题太水了

    苹果在树枝上,然后用子树更新节点的f数组即可。

    code

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<vector>
     5 using namespace std;
     6 
     7 const int Maxn = 110;
     8 
     9 struct Edge{
    10     int to,wi,ne;
    11 }edges[Maxn<<1];
    12 
    13 int f[Maxn][Maxn];
    14 int size[Maxn],wi[Maxn],first[Maxn];
    15 int q,n,x,y,z,cnte = 1;
    16 
    17 void add_edge(int fr,int to,int wi){
    18     edges[++cnte] = (Edge){to,wi,first[fr]};
    19     first[fr] = cnte;
    20 }
    21 
    22 void dp(int rt,int fa){
    23     size[rt] = 0;
    24     for(int i = first[rt];i;i = edges[i].ne){
    25         int u = edges[i].to;
    26         if(u != fa){
    27             dp(u,rt);
    28             size[rt] += size[u]+1;
    29             for(int j = q;j > 0;j--)
    30                 for(int k = 0;k <= size[u]&&j-k-1 >= 0;k++)
    31                     f[rt][j] = 
    32                         max(f[rt][j],f[u][k]+f[rt][j-k-1]+edges[i].wi);
    33         }
    34     }
    35 }
    36 
    37 int main(){
    38     cin >> n >> q;
    39     for(int i = 1;i < n;i++){
    40         cin >> x >> y >> z;
    41         add_edge(x,y,z);
    42         add_edge(y,x,z);
    43     }
    44     dp(1,0);
    45     cout << f[1][q];
    46 return 0;
    47 }
    View Code
  • 相关阅读:
    浅谈模块化开发
    用gulp搭建并发布自己的cli脚手架
    取值运算符新用法
    vue双向绑定之简易版
    获取对象属性之括号方式
    前端格式化工具之Prettier
    git操作之摘樱桃
    Sort
    MongoDB
    项目使用本地的包
  • 原文地址:https://www.cnblogs.com/Wangsheng5/p/11674813.html
Copyright © 2011-2022 走看看