zoukankan      html  css  js  c++  java
  • 树形背包(待续)

    洛谷 P2014 选课 树形依赖背包

    import java.util.*;
    public class Main1{
        static int MAXN=305;
        static int n,m,s[]=new int[MAXN],sz[]=new int[MAXN],dp[][]=new int[MAXN][MAXN];
        static Vector<Integer>[]g=new Vector[MAXN];
        public static void dfs(int u,int fa){
            sz[u]=1;dp[u][1]=s[u];
            for(int v:g[u]){
                if(v==fa)
                    continue;    
                dfs(v,u);
                sz[u]+=sz[v];
                for(int j=Math.min(m, sz[u]);j>=2;j--){
                    for(int k=1;k<=Math.min(j-1, sz[u]);j++){
                        dp[u][j]=Math.max(dp[u][j], dp[u][j-k]+dp[v][k]);
                    }
                }
            }
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
             m++;
             for(int i=1;i<=n;i++){
                 int k =sc.nextInt();
                 s[i]=sc.nextInt();
                 g[k].add(i);
                
                 g[i].add(k);
             }
             dfs(0,-1);
             System.out.println(dp[0][m]);
    }
        }
    }
  • 相关阅读:
    销售类
    语法
    编辑技巧 word
    assert
    游戏摘录
    游戏类链接 财富导图
    读书笔记之C# delegate
    c# socket传输struct类型
    关于wcf中一些重要词语解释
    asp.net CROSS-PAGE POSTING
  • 原文地址:https://www.cnblogs.com/ls-pankong/p/10496660.html
Copyright © 2011-2022 走看看