树形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. Pipe

    #一边压缩一边传 一边解压 到对方的目录为/tlj/2/ / | ssh root@172.16.200.56 tar xzf - -C /tlj #在一个需要文件名的地方 使用-重定向输出到stdo ...

  2. C#检测驱动是否安装的问题

    #region 检测CCD驱动是否安装成功 string path = @"C:\WINDOWS\system32\drivers\UsbCamIF.sys"; //驱动的默认安装 ...

  3. ofo走出校园观察:市场定位导致产品错位?

    Ofo和摩拜单车虽然同样都是做单车共享,但实际上两者在最初的市场定位是有明显的差异的,因此提供的产品方案也存在巨大的差异. 市场定位不同,导致产品方案的巨大差异 摩拜单车一开始就定位于开放市场,充分的 ...

  4. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

    今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...

  5. 系统配置 之:远程桌面连接(win7系统)

    本文包括两部分: 1.配置远程桌面连接 2.解决[远程桌面连接不上] 一.远程桌面连接设置 [远程桌面连接配置] Win7系统下的远程桌面连接设置,如果是其他系统或 Win8 及其以上系统,也可作为参 ...

  6. 逐行读取txt文件

    header("Content-type:text/html; charset=utf-8"); $handle = fopen('test.php', 'r') or die(' ...

  7. break; continue; goto; return在循环中的应用

    1. break表示跳出循环,程序指向循环体后的第一条语句: ; ) { ) break; console.writeline("{0}",i++); } console.read ...

  8. jquery 解析数据库中的json日期为正常的格式

    //在action从后台数据库中请求获得日期以后,得到的是json格式的数据,因此要解析才能显示在前台1.在jsp页面写的代码如下:<html> <script> Date.p ...

  9. UILabel UISwitch UISegmentedControl UIAlertView

    基础小控件 /***************************************UIlabel*************************************/ UILabel ...

  10. 安装php时的配置选项

    ./configure --prefix=/home/samba/anthony/web --enable-mbstring --enable-fastcgi --enable-fpm --with- ...