题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3201

  题意:给一颗树,每个节点有一个权值,求节点数为n的最大权子树。

  任意选择一个节点为根,然后DP转移就可以了,类似于分组背包。。。

 //STATUS:C++_AC_0MS_324KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e+,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End
int first[N],next[N*],val[N];
int f[N][N];
int n,m,mt,ans;
struct Edge{
int u,v;
}e[N*];
void adde(int a,int b)
{
e[mt].u=a,e[mt].v=b;
next[mt]=first[a],first[a]=mt++;
e[mt].u=b,e[mt].v=a;
next[mt]=first[b],first[b]=mt++;
}
void dfs(int u,int fa)
{
int i,j,k,v;
f[u][]=val[u];
for(i=first[u];i!=-;i=next[i]){
v=e[i].v;
if(v==fa)continue;
dfs(v,u);
for(j=m;j>;j--){
for(k=;k<j;k++){
f[u][j]=Max(f[u][j],f[u][j-k]+f[v][k]);
}
}
}
ans=Max(ans,f[u][m]);
return ;
}
int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b;
while(~scanf("%d%d",&n,&m))
{
for(i=;i<n;i++){
scanf("%d",&val[i]);
}
mem(first,-);mt=;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
adde(a,b);
}
mem(f,);ans=;
dfs(,-);
printf("%d\n",ans);
}
return ;
}

ZOJ-3201 Tree of Tree 树形DP的更多相关文章

  1. Codeforces 791D Bear and Tree Jump(树形DP)

    题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...

  2. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  3. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

  4. 【2019.8.20 NOIP模拟赛 T2】小B的树(tree)(树形DP)

    树形\(DP\) 考虑设\(f_{i,j,k}\)表示在\(i\)的子树内,从\(i\)向下的最长链长度为\(j\),\(i\)子树内直径长度为\(k\)的概率. 然后我们就能发现这个东西直接转移是几 ...

  5. 『You Are Given a Tree 整体分治 树形dp』

    You Are Given a Tree Description A tree is an undirected graph with exactly one simple path between ...

  6. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  7. Codeforces 980F Cactus to Tree 仙人掌 Tarjan 树形dp 单调队列

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF980F.html 题目传送门 - CF980F 题意 给定一个 $n$ 个节点 $m$ 条长为 $1$ 的边 ...

  8. [codeforces161D]Distance in Tree(点分治/树形dp)

    题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...

  9. Codeforces 1276D - Tree Elimination(树形 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 繁琐的简单树形 dp(大雾),要是现场肯定弃了去做 F 题 做了我一中午,写篇题解纪念下. 提供一种不太一样的思路. 首先碰到这样的题肯定 ...

  10. HDU5909 Tree Cutting(树形DP + FWT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T with n ve ...

随机推荐

  1. 4.cadence原理图,环境设置[原创]

    1.菜单介绍 创建工程,原理图纸 特殊点: 鼠标先点击1,,在选中1后点击2 在Tools菜单下 Annotate:自动编号 back Annotate: 回标 -- DRC规则检测 Create N ...

  2. 51nod 1049 1049 最大子段和 (dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 令 dp[i]表示为以a[i]结尾的最大子段和,则  dp[i]= ...

  3. find a filename from a filehandle in Perl

    my $filename='/tmp/tmp.txt';open my $fh, '>', $filename;my $fd = fileno $fh;print readlink(" ...

  4. 《OD学storm》20160827

    http://www.cnblogs.com/lujinhong2/p/4686512.html http://blog.csdn.net/paul_wei2008/article/details/2 ...

  5. CVS数据的导入和导出

    2.CSV导入/导出测试 package junit.test; import java.io.File; import java.util.ArrayList; import java.util.L ...

  6. 用FireMonkey写QQ皮肤

    这是运行在Windows平台的效果,同样不需要改一行代码就可以运行在Mac Os,并且效果完全相同: 用FireMonkey做界面速度非常快,其提供的Effect ,Filter,Animation等 ...

  7. Qt之QLabel

    简述 QLabel提供了一个文本或图像的显示,没有提供用户交互功能. 一个QLabel可以包含以下任意内容类型: 内容 设置 纯文本 使用setText()设置一个QString 富文本 使用setT ...

  8. 在 VC6 中使用 GdiPlus-使用

    下面用 VC6 来写一个 GdiPlus 的 Demo 工程 Step1:新建一个名为 Demo_GdiPlus 的 MFC AppWizard(exe) 工程 操作步骤:(1)主菜单File-> ...

  9. ajax连接数据库并操作数据库

    Response.Write("<script  type='text/javascript' language='javascript' >alert('用户名不能为空!请输入 ...

  10. BZOJ 4004 装备购买

    md有毒卡什么精度!!!! 最大线性无关组(线性基)可作为模板. #include<iostream> #include<cstdio> #include<cstring ...