题目链接: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. c#换ip代理源码

    很多朋友都想如何提高自己的网站流量,可是都没有什么好的办法 经过很长时间的研究,在C#中实现了,当然了,这部分代码其中一部分是网上的,不是原创. using System; using System. ...

  2. java之并发

    一.线程 在java中多线程执行任务,主要有两种方式,一种是通过继承Thread类,重写run方法,优点是比较方便的创建一个线程,缺点是java中每个类只能有一个基类,所有继承了T火热ad类后,就不能 ...

  3. Projective Texture的原理与实现

    http://blog.csdn.net/xukunn1226/article/details/775644 Projective Texture是比较常见的一种技术,实现起来代码也就区区的不过百行, ...

  4. CSS之剪切横幅

    简述 clip-path属性指定一个应用到元素上的剪切路径.应用在SVG中<clipPath>元素上的属性值可以完全运用在clip-path属性上.还可以使用CSS Shapes模块中的基 ...

  5. android事件系列-onTouch事件与手势操作

    提示记忆:应用流程:在Activity中对控件执行 view.setOnTouchListener( OnTouchListener i);实现里面的OnTouchListener 接口中的方法,重点 ...

  6. php设计模式之观察者模式

    观察者模式:能够更便利地创建查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能性. 利用这种模式可以方便地创建一个对象(观察者),其可以用来“监视”另一个对象(被观察者)的状态.这样,就可以在 ...

  7. django - from django.db.models import F - class F

    F() 的执行不经过 python解释器,不经过本机内存,是生成 SQL语句的执行. # Tintin filed a news story! reporter = Reporters.objects ...

  8. Java反射基本玩法

    三个主要的反射类 Class反射对象描述类语义结构,可以从Class对象中获取构造函数.成员变量.方法类等元素的反射对象,并以编程的方式通过这些反射对象对目标类对象进行操作.这些反射对象类在java. ...

  9. 安卓 Input Events(输入事件)

    在安卓中,有不止一种方法从你的应用截取用户交互事件.在你的用户界面中考虑事件,途径就是从用户界面中的一个指定的view对象中捕获事件.该view提供了这样做的方法. 在你用来组成你布局的不同的view ...

  10. Scala下载安装配置(Mac)

    ---恢复内容开始--- 1.访问scala的官网这里下载最新版的scala. 2.解压缩文件包,可将其移动至/usr/local/share下 1 mv /download/scalapath /u ...