CF1088F Ehab and a weird weight formula【倍增】
首先把点权归到边上,设点权较小的一个点是v,也就是(u,v)的边权是log2(dis(u,v))*a[v]+a[v]+a[u]
然后还有一个性质就是这棵树按点权最小点提起来就是一个堆
暴力是n^2的MST,然后考虑优化,按照点权从小到大加入生成树,那么每个点加进去的时候会连到点权比他小的点上
因为log2是上取整的,并且从根到一个点的链上的点权是单调递增的,所以一定是和这个点距离为2^k的点最好,所以倍增然后枚举这些点即可,注意最要要特判一下直接接在根下面的情况
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1000005;
int n,h[N],cnt,rt,mn=2e9,f[N][20];
long long a[N],ans;
struct qwe
{
int ne,to;
}e[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
void dfs(int u,int fa)
{
f[u][0]=fa;
int w=1;
long long mn=a[fa];
for(int i=1;i<=19;i++)
if(f[f[u][i-1]][i-1])
{
w=i;
f[u][i]=f[f[u][i-1]][i-1];
mn=min(mn,1ll*(i+1)*a[f[u][i]]);
}
mn=min(mn,1ll*(w+2)*a[rt]);
if(fa)
ans+=a[u]+mn;
for(int i=h[u];i;i=e[i].ne)
if(e[i].to!=fa)
dfs(e[i].to,u);
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
a[i]=read();
if(a[i]<mn)
rt=i,mn=a[i];
}
for(int i=1;i<n;i++)
{
int x=read(),y=read();
add(x,y),add(y,x);
}
dfs(rt,0);
printf("%lld\n",ans);
return 0;
}
CF1088F Ehab and a weird weight formula【倍增】的更多相关文章
- CF1088F Ehab and a weird weight formula 贪心 倍增
CF1088F Ehab and a weird weight formula 题意 给定一棵树,点有点权,其中这棵树满足除了权值最小的点外,每个点至少有一个点权小于它的相邻点. 要求你重新构建这棵树 ...
- CF1088F Ehab and a weird weight formula
CF1088F Ehab and a weird weight formula 推性质猜结论题 第一步转化,考虑把点的贡献加到边里: $con=\sum (log_2(dis(a_u,a_b))\ti ...
- Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula
F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...
- 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集
[题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...
- cf827D Best Edge Weight (kruskal+倍增lca+并查集)
先用kruskal处理出一个最小生成树 对于非树边,倍增找出两端点间的最大边权-1就是答案 对于树边,如果它能被替代,就要有一条非树边,两端点在树上的路径覆盖了这条树边,而且边权不大于这条树边 这里可 ...
- Codeforces Round #525 (Div. 2) Solution
A. Ehab and another construction problem Water. #include <bits/stdc++.h> using namespace std; ...
- CodeForces Round 525
A:Ehab and another construction problem #include<bits/stdc++.h> using namespace std; #define F ...
- System and method for cache management
Aspects of the invention relate to improvements to the Least Recently Used (LRU) cache replacement m ...
- Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法
You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loop ...
随机推荐
- 【python】使用python写windows服务
背景 运维windows服务器的同学都知道,windows服务器进行批量管理的时候非常麻烦,没有比较顺手的工具,虽然saltstack和ansible都能够支持windows操作,但是使用起来总感觉不 ...
- 更新TP-LINK路由器的外网IP到花生壳动态IP解析
------------------------------------------------------------------------------- 以下内容可能还是存在问题,等之后有时间再 ...
- CodeForces - 540B School Marks —— 贪心
题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...
- LightOJ - 1248 Dice (III) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1248 1248 - Dice (III) PDF (English) Statistics Forum Tim ...
- Mac OS X 下查看和设置JAVA_HOME
原文链接 : http://blog.csdn.net/done58/article/details/51138057 1, 查看Java版本 打开Mac电脑,查看JAVA版本,打开终端Termina ...
- ubuntn14.04 使用 nvm创建多版本node环境
1. 下载 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash 2. 然后 ...
- RQNOJ 117 最佳课题选择:多重背包
题目链接:https://www.rqnoj.cn/problem/117 题意: NaCN_JDavidQ要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择. 由于课题数有限,NaCN_JD ...
- chrome浏览器常用快捷键
chrome浏览器常用快捷键 一.总结 一句话总结: Ctrl + j:打开“下载内容”页 Ctrl + t:打开新的标签页,并跳转到该标签页 Ctrl + d:将当前网页保存为书签 1.在新标签页中 ...
- Log4j 与 logback对比、及使用配置
二.参考文档 1.Log4j 与 logback对比.及使用配置
- PG替换字段中的回车与换行
REPLACE(filed, CHR(10), '') //替换换行符 REPLACE(filed, CHR(13), '') //替换回车符