首先把点权归到边上,设点权较小的一个点是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【倍增】的更多相关文章

  1. CF1088F Ehab and a weird weight formula 贪心 倍增

    CF1088F Ehab and a weird weight formula 题意 给定一棵树,点有点权,其中这棵树满足除了权值最小的点外,每个点至少有一个点权小于它的相邻点. 要求你重新构建这棵树 ...

  2. CF1088F Ehab and a weird weight formula

    CF1088F Ehab and a weird weight formula 推性质猜结论题 第一步转化,考虑把点的贡献加到边里: $con=\sum (log_2(dis(a_u,a_b))\ti ...

  3. 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 题意: 给出一颗点有权值的树 ...

  4. 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集

    [题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...

  5. cf827D Best Edge Weight (kruskal+倍增lca+并查集)

    先用kruskal处理出一个最小生成树 对于非树边,倍增找出两端点间的最大边权-1就是答案 对于树边,如果它能被替代,就要有一条非树边,两端点在树上的路径覆盖了这条树边,而且边权不大于这条树边 这里可 ...

  6. Codeforces Round #525 (Div. 2) Solution

    A. Ehab and another construction problem Water. #include <bits/stdc++.h> using namespace std; ...

  7. CodeForces Round 525

    A:Ehab and another construction problem #include<bits/stdc++.h> using namespace std; #define F ...

  8. System and method for cache management

    Aspects of the invention relate to improvements to the Least Recently Used (LRU) cache replacement m ...

  9. 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 ...

随机推荐

  1. Zookeeper- Error contacting service. It is probably not running解决方案和原理

    搭建启动Zookeeper集群出现Error contacting service. It is probably not running解决方案和原理 1.关闭防火墙  [root@srv01 bi ...

  2. 团队作业第5周——测试与发布(Alpha版本)

    1.发现的bug a.同时按下和蛇前进方向相反的键和垂直方向的任意一个键贪吃蛇会死亡(比如贪吃蛇向右行走,同时按左上或左下都会game over) b.刷新的苹果会在蛇身上出现 暂时还没能力修复,以后 ...

  3. T59

    Working without a break makes you more prone to error. The great drawback to living near a main road ...

  4. PyNLPIR python中文分词工具

    官网:https://pynlpir.readthedocs.io/en/latest/  github:https://github.com/tsroten/pynlpir          NLP ...

  5. linux命令学习笔记(34):du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看, 还是和df命令有一些区别的. .命令格式: du [选项][文件] .命令功能: ...

  6. 主备角色switch

    理论知识:Switchover 切换允许primary 和一个备库进行切换,并且这种切换没有数据丢失. 前提条件: 1) 主备库相关参数 fal_client.fal_server .standby_ ...

  7. Vijos:P1117数的划分

    描述 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1;问有多少种不同的分法. 格式 输入 ...

  8. Scala学习——类,继承,接口(中)

    基本类的使用:(初) package com.dtspark.scala.basics /** * trait是一个接口 * 接口的第一次继承用extends,多继承时用with * 多继承时,如果这 ...

  9. 0001_第一个测试小程序Login

    # -*- coding:utf-8 -*- user = raw_input("Username:") password = raw_input("Password:& ...

  10. pysam操作sam文件

    pysam模块 因为要分析sam文件中序列的情况,因此要对reads进行细分,所以之前想用数据库将sam文件信息存储,然后用sql语句进行分类.后来发现很麻烦,pysam就是一个高效读取存储在SAM ...