ACdream1032 Component

题解:

树形dp+背包

不难想到,dp[i[j]表示以i为根的子树中,有j个子树的最小和

Size数组记录子树节点个数,然后就树上背包了

树上背包写时,在更新时,需要逆写

这里不懂,问了下q巨,QRZ

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=2050;
const int mod=1e9+7;
const ll INF=1e18;

ll dp[maxn][maxn],ans[maxn];
int Size[maxn],p[maxn],head[maxn];
int n,cnt;

struct Edge{
    int v,nxt;
}edge[maxn*2];

void Init(){
    cnt=0;
    memset(Size,0,sizeof(Size));
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++) dp[i][j]=INF;
    for(int i=1;i<=n;i++) ans[i]=INF;
}

void AddEdge(int u,int v){
    edge[cnt].v=v;
    edge[cnt].nxt=head[u];
    head[u]=cnt++;
}

void dfs(int u,int fa){
    dp[u][1]=p[u]*1LL;
    Size[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].nxt){
        int v=edge[i].v;
        if(v==fa) continue;
        dfs(v,u);
        Size[u]+=Size[v];
        for(int j=Size[u];j>=2;j--)
        for(int k=min(Size[v],j-1);k>=1;k--) dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]);
    }
}

int main(){
    int u,v;
    scanf("%d",&n);
    Init();
    for(int i=1;i<=n;i++) scanf("%d",&p[i]);
    for(int i=1;i<=n-1;i++){
        scanf("%d%d",&u,&v);
        AddEdge(u,v);AddEdge(v,u);
    }
    dfs(1,-1);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++) ans[j]=min(ans[j],dp[i][j]);
    for(int i=1;i<=n;i++) printf("%lld%c",ans[i],i==n?‘\n‘:‘ ‘);
    return 0;
}
时间: 2024-08-07 20:04:57

ACdream1032 Component的相关文章

【推介】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

Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

URL:http://www.ulewo.com/user/10001/blog/273 我们在使用spring的时候经常会用到这些注解,那么这些注解到底有什么区别呢.我们先来看代码 同样分三层来看: Action 层: package com.ulewo.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Co