ACdream 1032 Component

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

ftiasch

解题:树形dp

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2010;
 4 vector<int>g[maxn];
 5 int val[maxn],dp[maxn][maxn],n,son[maxn],ans[maxn];
 6 void dfs(int u,int fa){
 7     dp[u][1] = val[u];
 8     dp[u][0] = 0;
 9     son[u] = 1;
10     for(int i = g[u].size()-1; i >= 0; --i){
11         if(g[u][i] == fa) continue;
12         dfs(g[u][i],u);
13         son[u] += son[g[u][i]];
14         for(int j = son[u]; j > 0; --j){
15             for(int k = 1; k <= j; ++k)
16                 dp[u][j] = min(dp[u][j],dp[g[u][i]][j - k] + dp[u][k]);
17         }
18     }
19     for(int i = son[u]; i >= 0; --i)
20         ans[i] = min(ans[i],dp[u][i]);
21 }
22 int main(){
23     while(~scanf("%d",&n)){
24         for(int i = 0; i < maxn; ++i) g[i].clear();
25         for(int i = 1; i <= n; ++i) scanf("%d",val + i);
26         memset(dp,0x3f,sizeof dp);
27         memset(ans,0x3f,sizeof ans);
28         for(int i = 1,u,v; i < n; ++i){
29             scanf("%d%d",&u,&v);
30             g[u].push_back(v);
31             g[v].push_back(u);
32         }
33         dfs(1,-1);
34         for(int i = 1; i <= n; ++i)
35             printf("%d%c",ans[i],i == n?‘\n‘:‘ ‘);
36     }
37     return 0;
38 }

时间: 2024-11-10 16:16:01

ACdream 1032 Component的相关文章

acdream 1032 树形dp

又是一道树上做分组背包的题目... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int INF = 999999999; 7 const int N = 2001; 8 int value[N]; 9 int head[N]; 10 int dp[N][N]; 11 int cnt[N]; 12 int ans[N]; 13

【推介】TMS的控件之“TMS Unicode Component Pack”和“TMS Advanced Toolbars &amp; Menus”

TMS Unicode Component Pack是一款支持多语言的界面控件包,提供超过60种控件来为你的Delphi和C++Builder应用程序添加Unicode支持. 介绍: TMS Unicode Component Pack控制组件能让你在不终止Delphi.C++Builder或Windows 95/98/ME的情况下利用Windows NT/2000/XP/2003/Vista的Unicode功能开发应用程序.  注意:这些控制组件不会将Unicode功能添加到Windows 9

Applying GI PSU &quot;opatch auto&quot; fails with &quot;The opatch Component check failed&quot;

Applying GI PSU using "opatch auto" fails with "The opatch Component check failed" (文档 ID 1169036.1) APPLIES TO: Oracle Database - Enterprise Edition - Version 11.2.0.3 and laterInformation in this document applies to any platform.***C

主成分分析(Principal Component Analysis,PCA

主成分分析(Principal Component Analysis,PCA)是将多个变量通过线性变换以选出较少几个重要变量的多元统计分析方法. 原理:在用统计分析方法研究多变量的课题时,变量个数太多就会增加课题的复杂性.人们自然希望变量个数较少而得到的信息较多.在很多情形,变量之间是有一定的相关关系的,当两个变量之间有一定相关关系时,可以解释为这两个变量反映此课题的信息有一定的重叠.主成分分析是对于原先提出的所有变量,将重复的变量(关系紧密的变量)删去多余,建立尽可能少的新变量,使得这些新变量

Tomcat启动报错 Failed to start component [StandardServer[8005]]解决

SEVERE: The required Server component failed to start so Tomcat is unable to start. org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]] 之前在Eclipse上部署了Tomcat服务器,今天在MyEclipse上部署,结果Tomcat启动失败,报错.在网上搜了半天,有的说是因为端口被占用,有

Kaldi的nnet2 Component

FixedAffineComponent:类 LDA-like 的非相关转换,由标准的 weight matrix plus bias 组成(即Wx+b),通过标准的 stochastic gradient descent(非minibatch SGD?) 训练而来,使用 global learning rate AffineComponentPreconditionedOnline:为 FixedAffineComponent 的一种提炼,训练过程中不仅使用global learning ra

- Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead. 解决方案

<template> <div>{{hello}}</div> <button @click="addOne">add one</button> <button @click="minusOne">minus one</button> </template> 在*.vue组件里有这么一段. 报错信息: (Emitted value instead of an instan

[Preact] Use State and Props in the Component Render Function

Preact offers, in addition to the regular component API from React, the ability to access both props & state as function parameters to the render method. This lesson will cover an example of how to utilize this convenience along with how destructurin

Spring Boot 之注解@Component @ConfigurationProperties(prefix = &quot;sms&quot;)

从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 例如: 配置文件: sms.url=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX sms.appkey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX sms.secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX sms.signName=XXXXXXXXXXXXX