CF1088F Ehab and a weird weight formula
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的更多相关文章
- CF1088F Ehab and a weird weight formula 贪心 倍增
CF1088F Ehab and a weird weight formula 题意 给定一棵树,点有点权,其中这棵树满足除了权值最小的点外,每个点至少有一个点权小于它的相邻点. 要求你重新构建这棵树 ...
- CF1088F Ehab and a weird weight formula【倍增】
首先把点权归到边上,设点权较小的一个点是v,也就是(u,v)的边权是log2(dis(u,v))*a[v]+a[v]+a[u] 然后还有一个性质就是这棵树按点权最小点提起来就是一个堆 暴力是n^2的M ...
- 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 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 ...
- 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. ...
- R笔记(1):formula和Formula
#####开一个新的系列.关于R的一些笔记,就是遇到过的一些问题的简单整理.可能很基本,也可能没什么大的用处,作为一个记录而已.------------------------------------ ...
- 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 ...
随机推荐
- QT 引用之前项目模板导致计算速度严重下降
最近做RRT规划算法,在新建工程中测试时,每一个周期大概花费20MS,但是当我把算法移植到之前写的工程模板中时,计算效率相当低,变为500毫秒.期初是以为代码有问题,然后就逐句查找,发现代码并没有问题 ...
- Python中的动态继承
所谓动态继承,是指代码运行时再决定某个类的父类.某些场景下会用到,比如threading.Thread和multiprocessing.Process这两个类有很多同名的接口,可以实现某个子类动态继承 ...
- JavaScript--天猫数量输入框
<!DOCTYPE html> <head> <meta charset="utf-8" /> <title>无标题文档</t ...
- 用python爬虫抓站的一些技巧总结 zz
用python爬虫抓站的一些技巧总结 zz 学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本, ...
- Servlet过虑器
过滤器是在请求的预处理和后处理时调用的对象. 主要用于执行转换,日志记录,压缩,加解密,输入验证等过滤任务. servlet过滤器是可插拔的,即它在web.xml文件中定义,如果从web.xml文件中 ...
- 提高webpack的构建速度的几种方法概括
通过externals配置来提取常用库 利用DllPlugin和DllReferencePlugin预编译资源模块,通过DllPlugin来对那些我们引用但是绝对不会修改的npm包来进行预编译,再通过 ...
- ios程序员6级考试(答案和解释)
http://blog.sunnyxx.com/2014/03/06/ios_exam_0_key/ 我是前言 上次发了个ios程序员6级考试题 ,还在不断补充中,开个帖子配套写答案和解释. 1. 下 ...
- SDUT-3346_数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一个按先序输入的字符序列,如abd ...
- Oracle dbms_random包的用法
1.dbms_random.value方法 dbms_random是一个可以生成随机数值或者字符串的程序包.这个包有initialize().seed().terminate().value().no ...
- 解决ViewState过于庞大的问题
这里是我将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间.并且它很 小,可以说是磁盘随便转一圈就能同时读取好多ViewState ...