树形DP。。。。

Tree of Tree


Time Limit: 1 Second      Memory Limit: 32768 KB

You're given a tree with weights of each node, you need to find the maximum subtree of specified size of this tree.

Tree Definition 
A tree is a connected graph which contains no cycles.

Input

There are several test cases in the input.

The first line of each case are two integers N(1 <= N <= 100), K(1 <= K <= N), where N is the number of nodes of this tree, and K is the subtree's size, followed by a line with N nonnegative integers, where the k-th integer indicates the weight of k-th node. The following N - 1 lines describe the tree, each line are two integers which means there is an edge between these two nodes. All indices above are zero-base and it is guaranteed that the description of the tree is correct.

Output

One line with a single integer for each case, which is the total weights of the maximum subtree.

Sample Input

3 1
10 20 30
0 1
0 2
3 2
10 20 30
0 1
0 2

Sample Output

30
40


Author: LIU, Yaoting
Source: ZOJ Monthly, May 2009

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int dp[200][200],valu[200],cnt[200],ans,N,K;
vector<int> g[200];

int dfs(int u,int fa)
{
    cnt=1;
    for(int i=0;i<g.size();i++)
    {
        int v=g;
        if(v==fa) continue;
        cnt+=dfs(v,u);
    }
    for(int k=0;k<g.size();k++)
    {
        int v=g[k];
        if(v==fa) continue;
        for(int i=cnt;i>=1;i--)
        {
            for(int j=0;j<i&&j<=cnt[v];j++)
                dp=max(dp,dp[i-j]+dp[v][j]);
        }
    }
    if(cnt>=K)
    {
        ans=max(ans,dp[K]);
    }
    return cnt;
}

int main()
{
    while(scanf("%d%d",&N,&K)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=0;i<N;i++)
        {
            scanf("%d",valu+i);
            dp[1]=valu;
            g.clear();
        }
        for(int i=0;i<N-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g.push_back(a);
        }
        ans=0;dfs(0,-1);
        printf("%d\n",ans);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Emacs )

ZOJ 3201 Tree of Tree的更多相关文章

  1. ZOJ 3201

    id=15737" target="_blank">Tree of Tree Time Limit: 1000MS   Memory Limit: 32768KB ...

  2. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  3. B-tree/B+tree/B*tree [转]

    (原文出处:http://blog.csdn.net/hbhhww/article/details/8206846) B~树 1.前言: 动态查找树主要有:二叉查找树(Binary Search Tr ...

  4. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  5. [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树

    [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...

  6. easyui tree扩展tree方法获取目标节点的一级子节点

    Easyui tree扩展tree方法获取目标节点的一级子节点 /* 只返回目标节点的第一级子节点,具体的用法和getChildren方法是一样的 */ $.extend($.fn.tree.meth ...

  7. Tree - Decision Tree with sklearn source code

    After talking about Information theory, now let's come to one of its application - Decision Tree! No ...

  8. Binary Indexed Tree (Fenwick Tree)

    Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...

  9. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

随机推荐

  1. 判断请求是不是ajax

    public static bool IsAjaxRequest(HttpRequest request) { if (request == null) { throw new ArgumentNul ...

  2. [转]virtualenv and virtualenvwrapper

    转自 http://liuzhijun.iteye.com/blog/1872241 virtualenv virtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能 ...

  3. 解决Cookie乱码问题

    写了一个cookie的定义和获取,结果我输入中文后,页面报错 报错信息如下: type Exception report message An exception occurred processin ...

  4. POJ2187Beauty Contest(任意点的最远距离 + 凸包)

    题目链接 题意:就是给N个点的坐标,然后求任意两个点距离的平方最大的值 枚举超时. 当明白了 最远距离的两个点一定在凸包上,一切就好办了.求出凸包,然后枚举 #include <iostream ...

  5. Saltstack之Syndic(十)

    Saltstack之Syndic 使用条件: 1.salt syndic必须运行在一台master上 2.salt syndic必须依赖更高级的master 安装 yum install -y sal ...

  6. Knockoutjs的环境搭建教程

    最近要在项目中使用Knockoutjs,因此今天就首先研究了一下Knockoutjs的环境搭建,并进行了一个简单的测试,需要的朋友可以了解下 最近要在项目中使用Knockoutjs,因此今天就首先研究 ...

  7. python学习笔记-(三)条件判断和循环

    1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: age_of_cc = 27 age = int(input("guessage ...

  8. elasticsearch scroll api--jestclient invoke

    @Test public void testScroll(){ JestClientFactory factory = new JestClientFactory(); factory.setHttp ...

  9. Linux 的cp命令详解

    功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录, ...

  10. ef6 缓存的问题没有了

    public static void Main(string[] args) { PMTestEntities db = new PMTestEntities(); var users = db.Us ...