树形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. cookie的实例

    使得Cookie简化用户登陆,要求如下: 1.用户第一次登陆时需要输入用户名和密码 2.当登陆成功后,在Cookie中保存用户的登陆信息 3.设置Cookie有效期为5分钟 4.在有效期内用户再次登陆 ...

  2. WPF元素可视化效果

    在WPF中设置元素的可视化效果主要用到BlurEffect类和DropShadowEffect类.(目前只学到这两个,哈哈) 1.BlurEffect类 命名空间: System.Windows.Me ...

  3. 1 NFS高可用解决方案之DRBD+heartbeat搭建

    preface NFS作为业界常用的共享存储方案,被众多公司采用.我司也不列外,使用NFS作为共享存储,为前端WEB server提供服务,主要存储网页代码以及其他文件. 高可用方案 说道NFS,不得 ...

  4. Vector & ArrayList 的主要区别

    1) 同步性:Vector是线程安全的,也就是说是同步的 ,而ArrayList 是线程序不安全的,不是同步的 数2. 2)数据增长:当需要增长时,Vector默认增长为原来一倍 ,而ArrayLis ...

  5. BZOJ2124: 等差子序列

    题意:给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len>=3),使得 Ap1,Ap ...

  6. 【原】flux学习笔记

    最近React(web/native)依旧如火如荼,相信大家都跃跃欲试,入职新公司,现在的团队也开始在React领域有所尝试. 2016年应该是React 逐渐走向成熟的一年.之前在原来公司搞不懂的问 ...

  7. MySQL------如何将SQLServer文件数据迁移到MySQL

    转载: http://blog.csdn.net/zhangdaiscott/article/details/46412453

  8. IM即时通讯

    即时通讯,由于项目需求和不可抗力因素用的融云 当然我更倾向于用环信亲加等 使用融云遇到的那些坑: 1.集成时的坑: ,编译环境要求太高    项目中有很多旧的东西  达不到其标准 直接用最新版,出错, ...

  9. php Hash Table(一) Hash Table的结构

    关于Hash Table专题: 一直想深入理解一下php的hash table的实现,以前一直是星星点点的看看,从未彻底的总结过,那就从这个专题开始吧! 主要想总结几个部分:hashtable结构,h ...

  10. asp.net(C#)页面事件顺序

    asp.net(C#)页面事件顺序 http://www.cnblogs.com/henw/archive/2012/02/09/2343994.html   1 using System.Data; ...