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 ;
}

最新文章

  1. TFS 分支导致nuget项目依赖丢失
  2. Qt 静态编译后的exe太大, 可以这样压缩.
  3. 搜索栏会消失 uisearchbar 狂点消失的问题解决
  4. Webmin
  5. apue第六章学习总结
  6. C++数据类型总结
  7. Entity Framework 学习笔记(一)安装
  8. 【54】让自己熟悉包括TR1在内的标准程序库
  9. 关于日历控件My97DatePicker 在IE6下出现“无法打开站点,已终止操作”
  10. linux之SQL语句简明教程---IN
  11. 为linux系统实现回收站
  12. 自学Zabbix3.10.1.1-事件通知Notifications upon events-媒介类型email
  13. Kudu系列-基础
  14. 【慕课网实战】Spark Streaming实时流处理项目实战笔记十八之铭文升级版
  15. version control(关于版本控制)
  16. 最顶尖的12个IT技能
  17. 网页关闭(解决window.close在火狐下不兼容问题)
  18. idea git pull项目到本地时容易出现的问题
  19. python的内置模块random随机模块方法详解以及使用案例(五位数随机验证码的实现)
  20. element-ui中使用font-awesome字体图标

热门文章

  1. Kernel trick----PRML读书笔记
  2. la3713
  3. flex中align-content属性
  4. 题解报告:hdu 2149 Public Sale(巴什博弈)
  5. scala的Class
  6. jq-文本框只能输入数字
  7. Jquery 可拖拽的Ztree
  8. 1B课程笔记分享_StudyJams_2017
  9. 《Java编程的逻辑》第二部分 面向对象
  10. TensorFlow-Gpu环境搭建——Win10+ Python+Anaconda+cuda