/*
因为权值都是正的, 所以贪心的正确性能保证
然后重链贪心跑一下就好了 */
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define M 200010
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
vector<int> to[M];
ll ver[M], sta[M], tp, top[M], son[M], n, k; void dfs(int now, int fa) {
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa) continue;
dfs(vj, now);
if(ver[vj] > ver[son[now]]) son[now] = vj;
}
ver[now] += ver[son[now]];
} void dfss(int now, int fa) {
if(top[now] == now) sta[++tp] = ver[now];
if(son[now]) {
top[son[now]] = top[now];
dfss(son[now], now);
}
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == fa || vj == son[now]) continue;
top[vj] = vj;
dfss(vj, now);
}
} int main() {
n = read(), k = read();
for(int i = 1; i <= n; i++) ver[i] = read();
for(int i = 1; i < n; i++) {
int vi = read(), vj = read();
to[vi].push_back(vj);
to[vj].push_back(vi);
}
dfs(1, 1);
top[1] = 1;
dfss(1, 1);
sort(sta + 1, sta + tp + 1, greater<ll>() );
ll ans = 0;
for(int i = 1; i <= k; i++) ans += sta[i];
cout << ans << "\n";
return 0;
}

bzoj3252: 攻略(贪心)的更多相关文章

  1. BZOJ3252 攻略 贪心、长链剖分

    传送门 给树竟直接给父子关系!!!真良心 首先一个贪心策略:每一次选择的链一定是所有链中权值最大的.这应该比较显然 那么我们接下来考虑如何维护这个贪心.我们可以使用长链剖分进行维护,对权值进行长链剖分 ...

  2. bzoj3252 攻略 贪心+dfs序+线段树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3252 题解 有一个非常显然的贪心思路:每次选择目前走到那儿能够获得的新权值最大的点. 证明的话 ...

  3. [bzoj3252]攻略_dfs序_线段树_贪心

    攻略 bzoj-3252 题目大意:给定一棵n个节点的有根树,点有点权.让你选出至多k个节点,使得他们到根的链的并最大. 注释:$1\le n\le 2\cdot 10^5$,$1\le val_i\ ...

  4. 【bzoj3252】攻略 贪心+DFS序+线段树

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...

  5. BZOJ3252攻略——长链剖分+贪心

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(scene),某 ...

  6. BZOJ3252: 攻略

    Description 题目简述:树版[k取方格数]   众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景 ...

  7. BZOJ3252 攻略 [树链剖分]

    题目传送门 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1169  Solved: 554[Submit][Status][Discuss] ...

  8. bzoj3252 攻略 dfs序+线段树

    题目传送门 题目大意:给出一棵树,1为根节点,每个节点都有权值,每个叶子节点都是一个游戏的结局,选择k个游戏结局,使得权值总和最大,同一个节点不会被重复计算. 思路:这道题最关键的是要想到一个性质,就 ...

  9. bzoj3252攻略(线段树+dfs序)

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 562  Solved: 238[Submit][Status][Discuss] D ...

随机推荐

  1. uglifyjs-webpack-plugin 插件,drop_console 默认为 false(不清除 console 语句),drop_debugger 默认为 true(清除 debugger 语句)

    uglifyjs-webpack-plugin 插件,drop_console 默认为 false(不清除console语句),drop_debugger 默认为 true(清除 debugger 语 ...

  2. mysql 5.45 以后需要 需要 安全套接字问题

    错误异常:According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established ...

  3. log4j.properties配置说明学习网址

    https://blog.csdn.net/wangzhaotongalex/article/details/51308802

  4. dashboard无法查到endpoint, counter

    问题 使用Open-falcon时,Agent 推送metrics后,无法在Dashboard上显示.将Agent的hostname 修改后,却可以. 解决办法 查看Agent log,推送到tran ...

  5. MMCM与PLL

    MMCM与PLL   1.the clock management title(CMT) 弄清楚BUFR, IBUFG,BUFG,GT,BUFH,是什么. 2.MMCM内部结构 3.PLL内部结构 4 ...

  6. linux(ubuntu)下安装phantomjs

    1.安装phantomjs ubuntu下sudo apt-get install phantomjs下载的不能用 —-下载程序文件 到官网下载 1.安装phantomjs —-下载程序文件 wget ...

  7. 【maven】之使用import scope解决maven继承(单)问题

    想必大家在做SpringBoot应用的时候,都会有如下代码: <parent> <groupId>org.springframework.boot</groupId> ...

  8. MIME Types - Complete List

    Suffixes applicable Media type and subtype(s) .3dm x-world/x-3dmf .3dmf x-world/x-3dmf .a applicatio ...

  9. 学习笔记之The Intelligent Investor, Rev. Ed

    The Intelligent Investor, Rev. Ed https://www.safaribooksonline.com/library/view/the-intelligent-inv ...

  10. HTML背景图片自适应

    由于<body>标签的图片不能够拉伸, 解决办法: 1.图片不够大,又background属性不能拉伸图片: 2.只能用个div,把其z-index值设为负,并使这个div大小为整个bod ...