CF1088F Ehab and a weird weight formula

推性质猜结论题

第一步转化,考虑把点的贡献加到边里:

$con=\sum (log_2(dis(a_u,a_b))\times min(a_u,a_v))+a_u+a_v$

然后一个结论:

一个点最多有一个相邻的点比它小

因为会连出一串,只能在唯一的最小值点结束

所以,以最小值为根,建出有根树,每个点的fa就是比它小的

整个树越往祖先权值越小

不妨再给边定向,令边的方向就是:$a_u>a_v,a_u->a_v$,

每个点只会连出去一条边,所以只用最小化:$(log_2(dis(a_u,a_v))+1)\times a_v$

发现,连出的边只会是往祖先连,否则dis会更大

而log_2是上去整,所以一定是$2^k$级祖先连过去最优!

注意如果不存在$2^k$级祖先,那么和$rt$也要试着连一连

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{
const int N=5e5+;
const ll inf=0x3f3f3f3f3f3f3f3f;
ll ans;
int n;
struct node{
int nxt,to;
}e[*N];
int hd[N],cnt;
int a[N];
void add(int x,int y){
e[++cnt].nxt=hd[x];
e[cnt].to=y;
hd[x]=cnt;
}
int fa[N][];
int dfs(int x){
for(reg i=hd[x];i;i=e[i].nxt){
int y=e[i].to;
if(y==fa[x][]) continue;
fa[y][]=x;
dfs(y);
}
}
int main(){
rd(n);
int rt=;
for(reg i=;i<=n;++i) {
rd(a[i]);
if(!rt||a[i]<a[rt]) rt=i;
}
int x,y;
for(reg i=;i<n;++i){
rd(x);rd(y);add(x,y);add(y,x);
}
dfs(rt);
for(reg j=;j<=;++j){
for(reg i=;i<=n;++i){
fa[i][j]=fa[fa[i][j-]][j-];
}
}
for(reg i=;i<=n;++i){
ll mi=inf;
if(i==rt) continue;
for(reg j=;j<=;++j){
if(!fa[i][j]) {
mi=min(mi,(ll)(j+)*a[rt]);
break;
}
mi=min(mi,(ll)(j+)*a[fa[i][j]]);
}
ans+=mi+a[i];
}
ot(ans);
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
*/

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【倍增】

    首先把点权归到边上,设点权较小的一个点是v,也就是(u,v)的边权是log2(dis(u,v))*a[v]+a[v]+a[u] 然后还有一个性质就是这棵树按点权最小点提起来就是一个堆 暴力是n^2的M ...

  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 Round #525 (Div. 2) Solution

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

  5. CodeForces Round 525

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

  6. System and method for cache management

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

  7. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-001Mapping basic properties(@Basic、@Access、access="noop"、@Formula、@ColumnTransformer、@Generated、 @ColumnDefaul、@Temporal、@Enumerated)

    一.简介 在JPA中,默认所有属性都会persist,属性要属于以下3种情况,Hibernate在启动时会报错 1.java基本类型或包装类 2.有注解 @Embedded 3.有实现java.io. ...

  8. R笔记(1):formula和Formula

    #####开一个新的系列.关于R的一些笔记,就是遇到过的一些问题的简单整理.可能很基本,也可能没什么大的用处,作为一个记录而已.------------------------------------ ...

  9. CF959E Mahmoud and Ehab and the xor-MST 思维

    Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem t ...

随机推荐

  1. 1.2开发文档简读,了解全貌.mp4

  2. 阿里云DDoS高防的演进:防御效果成核心

    分布式拒绝服务(DDoS)攻击这一网络公敌,是任何互联网业务的重大威胁.随着DDoS攻击工具化的发展,无论是简单野蛮的流量型攻击,还是复杂精巧的应用型攻击,黑客发起DDoS攻击变得越来越简单和自动化. ...

  3. WPF自定义控件 依赖属性绑定

    控件cs文件 using System.ComponentModel; using System.Windows; using System.Windows.Controls; using Syste ...

  4. yum方式安装MySQL【转】

    在CentOS7中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 另外至2919年5月4号, 默认安装的my ...

  5. UVa 10599【lis dp,记忆化搜索】

    UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...

  6. @codeforces - 913F@ Strongly Connected Tournament

    目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个选手参加了一场竞赛,这场竞赛的规则如下: 1.一开始,所有 ...

  7. QQ第三方登录报错error=-1

    qq 第三方登录报错error=-1 再次实例化qc类.

  8. 受控组件 & 非受控组件

    在 React 中表单组件可分为两类,受控与非受控组件. 一. 受控组件 设置了 value 的 <input> 是一个受控组件. 对于受控的 <input>,渲染出来的 HT ...

  9. 用CSS画平行四边形

    <div class="jx">我是一个平行四边形</div> <style> .jx{ //定义div的高度宽度 transform:skew ...

  10. Python基础知识汇总

    1.执行脚本的两种方式 Python a.py     直接调用Python解释器执行文件 chomd +x a.py   ./a.py    #修改a.py文件的属性,为可执行,在用  ./  执行 ...