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. poj3737 UmBasketella 真正的三分

    之前用二分写三分的板子...现在正式写一个三分,但是也不难,就是把区间分为三段就行了.求二次函数的峰值,每次取大的区间就行了. 题干: 最近几天,人们总是设计出多功能的新东西.例如,您不仅可以使用手机 ...

  2. POJ3090 巧用欧拉函数 phi(x)

    POJ3090 给定一个坐标系范围 求不同的整数方向个数 分析: 除了三个特殊方向(y轴方向 x轴方向 (1,1)方向)其他方向的最小向量表示(x,y)必然互质 所以对欧拉函数前N项求和 乘2(关于( ...

  3. [Swift通天遁地]六、智能布局-(1)给视图添加尺寸和中心点的约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. javascript 处理链接的多种方式

    在页面中的链接除了常规的方式以外,如果使用javascript,还有很多种方式,下面是一些使用javascript,打开链接的几种方式: 1.使用window的open方法打开链接,这里可是在制定页面 ...

  5. html5: table表格与页面布局整理

    传统表格布局之table标签排版总结:   默认样式: <style> table { max-width: 800px; border-spacing: 2px; border-coll ...

  6. javascript中for...in和for...of的区别

    for...of循环是ES6引入的新的语法. for...in遍历拿到的x是键(下标).而for...of遍历拿到的x是值,但在对象中会提示不是一个迭代器报错.例子如下: let x; let a = ...

  7. 【USACO2002 Feb】奶牛自行车队

    [USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...

  8. sqlyog注册码激活

    姓     名(Name):ttrar 序 列 号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c 或者(OR) 姓     名(Name):ttrar 序 列 ...

  9. 318 Maximum Product of Word Lengths 最大单词长度乘积

    给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...

  10. Unity学习-鼠标的常用操作(八)

    本次主要介绍5个鼠标事件 void OnMouseEnter():鼠标进入 void OnMouseExit():鼠标移出 void OnMouseDown():鼠标点击 void OnMouseUp ...