首页 技术 正文
技术 2022年11月21日
0 收藏 393 点赞 4,448 浏览 1063 个字

传送门

解题思路

  比较好想的一道思路题,结果有个地方没开\(long\) \(long\) \(wa\)了三次。。其实就是模仿一下树链剖分,重新定义重儿子,一个点的重儿子为所有儿子中到叶节点权值最大的点,然后就和树链剖分一样\(dfs\)一遍,把那些链的顶端的\(sum\)值放到一个数组排个序。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>using namespace std;
const int MAXN = 200005;
typedef long long LL;inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
}int n,k,head[MAXN],cnt,to[MAXN],nxt[MAXN],fa[MAXN];
int w[MAXN],son[MAXN],num;
LL ans,sum[MAXN],tmp[MAXN];inline void add(int bg,int ed){
to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
}inline bool cmp(LL x,LL y){
return x>y;
}void dfs1(int x){
LL maxson=0,u;sum[x]=w[x];
for(int i=head[x];i;i=nxt[i]){
u=to[i];dfs1(u);
if(sum[u]>maxson) {maxson=sum[u];son[x]=u;}
}
sum[x]+=maxson;
}signed main(){
n=rd(),k=rd();int x,y;
for(int i=1;i<=n;i++) w[i]=rd();
for(int i=1;i<n;i++){
x=rd(),y=rd();fa[y]=x;
add(x,y);
}
dfs1(1);
for(int i=1;i<=n;i++) if(i!=son[fa[i]]) tmp[++num]=sum[i];
sort(tmp+1,tmp+1+num,cmp);
for(int i=1;i<=k;i++) ans+=tmp[i];
printf("%lld\n",ans);
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849