博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[十二省联考2019]春节十二响
阅读量:7033 次
发布时间:2019-06-28

本文共 1772 字,大约阅读时间需要 5 分钟。

https://www.luogu.org/problemnew/show/P5290

神tm一道蓝题,我考试的时候都在想啥...

考虑一条链,显然你是把两个链分别的最大值放在一起,次大值放在一起,等等

那么如果有多个链呢?你就把第一个链和第二个链按上面的操作,得到的新的结果再和第三个链合并...

正确性挺显然的...qwq

复杂度O(nlog^2n)

#include
#include
#include
#include
#include
using namespace std;#define O(x) cout << #x << " " << x << endl;#define B cout << "breakpoint" << endl;inline int read(){ int ans = 0,op = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') op = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { (ans *= 10) += ch - '0'; ch = getchar(); } return ans * op;}typedef long long ll;const int maxn = 2e5 + 5;struct node{ int to,next,cost;}e[maxn << 1];int fir[maxn],alloc;void adde(int u,int v){ e[++alloc].next = fir[u]; fir[u] = alloc; e[alloc].to = v; swap(u,v); e[++alloc].next = fir[u]; fir[u] = alloc; e[alloc].to = v;}int a[maxn];int id[maxn],cnt,tp[maxn];priority_queue
q[maxn];void dfs(int u,int fa){ //O(u); id[u] = ++cnt; for(int i = fir[u];i;i = e[i].next) { int v = e[i].to; if(v == fa) continue; dfs(v,u); if(q[id[u]].size() < q[id[v]].size()) swap(id[u],id[v]); int tot = q[id[v]].size(); for(int i = 1;i <= tot;i++) { tp[i] = max(q[id[u]].top(),q[id[v]].top()); q[id[u]].pop(),q[id[v]].pop(); } for(int i = 1;i <= tot;i++) q[id[u]].push(tp[i]); } q[id[u]].push(a[u]);}int main(){ int n = read(); for(int i = 1;i <= n;i++) a[i] = read(); for(int i = 2;i <= n;i++) { int f = read(); adde(f,i);} dfs(1,0); ll ans = 0; while(q[id[1]].size()) ans += q[id[1]].top(),q[id[1]].pop(); printf("%lld",ans);}

 

转载于:https://www.cnblogs.com/LM-LBG/p/10691743.html

你可能感兴趣的文章
SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
查看>>
Umeng第三方登录
查看>>
EggBorn.js:一款顶级Javascript全栈开发框架
查看>>
前端开始的那件事——表单
查看>>
【前端】HTML属性
查看>>
js 算法3
查看>>
【Java 容器面试题】谈谈你对HashMap 的理解
查看>>
分组圆角TableView
查看>>
高级Java研发者在解决大数据问题上的一些技巧
查看>>
用 Node 开发一个命令行版本词典--不到十行的代码
查看>>
支持多解码模块的安卓视频播放器AndroidVideoplayer
查看>>
TCP协议详解
查看>>
Node.js process 模块解读
查看>>
Lodash源码分析-compact.js
查看>>
度小满牵手南京银行打造”AI鑫”计划:银行零售业掀起变革运动
查看>>
微信小程序之分享海报生成
查看>>
敏捷AI|NLP技术在宜信业务中的实践「背景篇」
查看>>
布局结束检测工具
查看>>
[MetalKit]21-What's-new-in-graphics-and-games-at-WWDC-2016
查看>>
html2canvas在vue下的巨坑
查看>>