Component

Time Limit: 5000ms
Memory Limit: 64000KB

This problem will be judged on ACdream. Original ID: 1032
64-bit integer IO format: %lld      Java class name: (No Java Yet)

 

Given a tree with weight assigned to nodes, find out minimum total weight connected component with fixed number of node.

 

Input

The first line contains a single integer n.

The second line contains n integers $w_1,w_2,…,w_n. w_i$ denotes the weight of the node i.

The following (n−1) lines with two integers ai and bi, which denote the edge between ai and bi.

Note that the nodes are labled by $1,2,…,n.$

$(1\leq n\leq 2⋅10^3,1\leq w_i\leq 10^5)$

Output

$n$ integers $c_1,c_2,…,c_n. c_i$ stands for the minimum total weight component with i nodes.

Sample Input

3
1 2 3
1 2
2 3

Sample Output

1 3 6

Source

 
解题:树形dp
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
vector<int>g[maxn];
int val[maxn],dp[maxn][maxn],n,son[maxn],ans[maxn];
void dfs(int u,int fa){
dp[u][] = val[u];
dp[u][] = ;
son[u] = ;
for(int i = g[u].size()-; i >= ; --i){
if(g[u][i] == fa) continue;
dfs(g[u][i],u);
son[u] += son[g[u][i]];
for(int j = son[u]; j > ; --j){
for(int k = ; k <= j; ++k)
dp[u][j] = min(dp[u][j],dp[g[u][i]][j - k] + dp[u][k]);
}
}
for(int i = son[u]; i >= ; --i)
ans[i] = min(ans[i],dp[u][i]);
}
int main(){
while(~scanf("%d",&n)){
for(int i = ; i < maxn; ++i) g[i].clear();
for(int i = ; i <= n; ++i) scanf("%d",val + i);
memset(dp,0x3f,sizeof dp);
memset(ans,0x3f,sizeof ans);
for(int i = ,u,v; i < n; ++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(,-);
for(int i = ; i <= n; ++i)
printf("%d%c",ans[i],i == n?'\n':' ');
}
return ;
}

ACdream 1032 Component的更多相关文章

  1. openfire的组件(Component)开发

    在之前的文章<Openfire阶段实践总结>中提到过一种openfire的扩展模式Compoent.本文将主要探讨对这种模式的应用与开发方法. 内部与外部组件介绍 在openfire中的许 ...

  2. salesforce 零基础学习(六十一)apex:component简单使用以及图片轮转播放的实现

    有的时候,我们项目有可能有类似需求:做一个简单的图像轮转播放功能,不同的VF页面调用可以显示不同的图片以及不同的图片描述.这种情况,如果在每个页面单独处理相关的图像轮转播放则显得代码特别冗余,此种情况 ...

  3. angular2 service component

    [component 需要通过 service 提供的接口 得到一些数据.这是最佳实践.] [由于 有 component 和 service 两个语义,所以出现了下面两种办法] 一,[service ...

  4. knockoutjs如何动态加载外部的file作为component中的template数据源

    玩过knockoutjs的都知道,有一个强大的功能叫做component,而这个component有个牛逼的地方就是拥有自己的viewmodel和template, 比如下面这样: ko.compon ...

  5. 解读ASP.NET 5 & MVC6系列(14):View Component

    在之前的MVC中,我们经常需要类似一种小部件的功能,通常我们都是使用Partial View来实现,因为MVC中没有类似Web Forms中的WebControl的功能.但在MVC6中,这一功能得到了 ...

  6. [转]ExtJs基础--Html DOM、Ext Element及Component三者之间的区别

    要学习及应用好Ext框架,必须需要理解Html DOM.Ext Element及Component三者之间的区别. 每一个HTML页面都有一个层次分明的DOM树模型,浏览器中的所有内容都有相应的DOM ...

  7. component

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

  8. OleDb Source component 用法

    OleDb Source component 主要是从DB中获取数据,传递给下游组件,OleDb Source component的强大之处在于 query data 的mode有四种,如图 Tabl ...

  9. Script component 用法

    在SSIS中,可以使用C#编写脚本,这是十分激动人心的事,能够使用C#代码,使得Script Component无所不能. 第一部分:组件简介Script Component 有三种类型:Source ...

随机推荐

  1. Silverlight访问数据库大全(转)

    Silverlight访问数据库大全 Silverlight访问数据库大全 Posted on 2010-06-13 17:25 moss_tan_jun 阅读(1917) 评论(0) 编辑 收藏 最 ...

  2. unsigned 赋值负数输出情况 & printf输出格式

    %d 有符号10进制整数 %ld 长整型 %hd短整型 %i 有符号10进制整数 %o 无符号8进制整数 %u 无符号10进制整数 %x 无符号的16进制数字,并以小写abcdef表示 %X 无符号的 ...

  3. App上架流程 & 上架被拒10大原因

    上架前预热 先登陆自己的开发者账号(自己提前注册好 iOS 开发者账号,这里假设你已经拥有了一个 iOS 开发者账号),进入这个页面:https://developer.apple.com/accou ...

  4. Python机器学习算法 — K-Means聚类

    K-Means简介 步,直到每个簇的中心基本不再变化: 6)将结果输出. K-Means的说明 如图所示,数据样本用圆点表示,每个簇的中心点用叉叉表示:       (a)刚开始时是原始数据,杂乱无章 ...

  5. HTTP Status 500 - Could not write content: could not initialize proxy - no Session

    分析出现no Session错误的原因以及给出解决方案: 使用SpringMVC + JSON数据返回时,经常会出现no Session的错误: 报错原因:因为懒加载在提取关联对象的属性值的时候发现E ...

  6. asp.net MVC ajax 请求参数前台加密后台解密

    最近有一个需求要求页面查询数据库,查询内容保存到excel里面作为附件加密打包下载.查询的sql作为参数传入后台,实现加密提交.这里做个记录,后面用到直接来拿. 控制器 public ActionRe ...

  7. easyui- comobo 详细讲解

    具体代码实现 @{    ViewBag.Title = "人员查找";    ViewBag.LeftWidth = "200px";    ViewBag. ...

  8. flask 中的模板语法 jinja2及render_template的深度用法

    是时候开始写个前端了,Flask中默认的模板语言是Jinja2 现在我们来一步一步的学习一下 Jinja2 捎带手把 render_template 中留下的疑问解决一下 首先我们要在后端定义几个字符 ...

  9. 未能加载文件或程序集Microsoft.SharePoint.Sandbox.dll

    项目引用了MiscroSoft.SharePoint.dll程序集,编译后页面报错: 未能加载文件或程序集“Microsoft.Sharepoint.Sandbox, Version=14.0.0.0 ...

  10. 控制台——EventLog实现事件日志操作

    我们应该如何通过写代码的方式向其中添加“日志”呢? 在操作之前,先明确几个概念: 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“ ...