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