ACdream 1032 Component
Component
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
#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的更多相关文章
- openfire的组件(Component)开发
在之前的文章<Openfire阶段实践总结>中提到过一种openfire的扩展模式Compoent.本文将主要探讨对这种模式的应用与开发方法. 内部与外部组件介绍 在openfire中的许 ...
- salesforce 零基础学习(六十一)apex:component简单使用以及图片轮转播放的实现
有的时候,我们项目有可能有类似需求:做一个简单的图像轮转播放功能,不同的VF页面调用可以显示不同的图片以及不同的图片描述.这种情况,如果在每个页面单独处理相关的图像轮转播放则显得代码特别冗余,此种情况 ...
- angular2 service component
[component 需要通过 service 提供的接口 得到一些数据.这是最佳实践.] [由于 有 component 和 service 两个语义,所以出现了下面两种办法] 一,[service ...
- knockoutjs如何动态加载外部的file作为component中的template数据源
玩过knockoutjs的都知道,有一个强大的功能叫做component,而这个component有个牛逼的地方就是拥有自己的viewmodel和template, 比如下面这样: ko.compon ...
- 解读ASP.NET 5 & MVC6系列(14):View Component
在之前的MVC中,我们经常需要类似一种小部件的功能,通常我们都是使用Partial View来实现,因为MVC中没有类似Web Forms中的WebControl的功能.但在MVC6中,这一功能得到了 ...
- [转]ExtJs基础--Html DOM、Ext Element及Component三者之间的区别
要学习及应用好Ext框架,必须需要理解Html DOM.Ext Element及Component三者之间的区别. 每一个HTML页面都有一个层次分明的DOM树模型,浏览器中的所有内容都有相应的DOM ...
- component
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...
- OleDb Source component 用法
OleDb Source component 主要是从DB中获取数据,传递给下游组件,OleDb Source component的强大之处在于 query data 的mode有四种,如图 Tabl ...
- Script component 用法
在SSIS中,可以使用C#编写脚本,这是十分激动人心的事,能够使用C#代码,使得Script Component无所不能. 第一部分:组件简介Script Component 有三种类型:Source ...
随机推荐
- [Codeforces Round495A] Sonya and Hotels
[题目链接] https://codeforces.com/contest/1004/problem/A [算法] 直接按题意模拟即可 时间复杂度 :O(NlogN) [代码] #include< ...
- C. Unusual Product(cf)
http://codeforces.com/problemset/problem/405/C 题意: 给出一个n*n的矩阵,有q个操作,输入3时,输出A ,A等于第i行乘以第i列的对应元素的和(mod ...
- [App Store Connect帮助]三、管理 App 和版本(2.1)输入 App 信息:查看和编辑 App 信息
在您添加 App 至您的帐户后,您也可以在“我的 App”部分查看和编辑 App 信息和平台版本信息. 在输入 App 信息前,请检查必填项.可本地化和可编辑属性.您在上传构建版本或提交您的 App ...
- [Swift通天遁地]七、数据与安全-(16)检测Apple设备是否越狱
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- easyui -tree的详细讲解
代码的具体实现 @{ ViewBag.Title = "人员查找"; ViewBag.LeftWidth = "200px"; ViewBag ...
- BZOJ 4867 分块+神tm卡常
思路: 注意到len<=10 按照权值max-min<=sqrt(n)*len 分块 记一下前缀和 每修改sqrt(n)次以后重新分块 修改的时候整块打标记 两边重构 (这题常数卡 ...
- day02_12/12/2016_bean的实例化之普通工厂方式
- go语言Notepad++简易开发环境搭建(windows)
1.下载安装go语言:https://golang.org/dl/选择对应的平台,建议使用msi安装包,这个会帮你配置好环境变量(也许需要重启)对应的环境变量有: GOROOT - C:\Go\PAT ...
- MVC系列学习(十七)-过滤器
本次学习的文件结构如下 1.过滤器的几种表示方式 1.1将过滤器 加到方法上,作用范围为该方法 1.2将过滤器加到当前类上,作用范围为该类的所有方法 1.3添加全局过滤器,作用范围为所有方法 2.Ac ...
- Java中的overload(方法的覆写)
方法覆写(overload)与方法的重载非常相似,它在 Java的继承中也有很重要的应用. 写程序可能会碰到下面的情况,在父类中已经实现的方法可能不够精确,不能满足子类 的需求.例如在前面的 Anim ...