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 ...
随机推荐
- 【风马一族_php】PHP运算
运算 算术运算符 <?php //加法 $num1 = 10; $num2 = 43; echo $num1 + $num2; echo " "; var_dump($num ...
- Effective Modern C++:03转向现代C++
07:在创建对象时注意区分()和{} 自C++11以来,指定初始化值的的方式包括使用小括号,等号,以及大括号: ); // initializer is in parentheses ; // ini ...
- Effective C++: 06继承与面向对象设计
32:确定你的public继承塑模出is-a关系 以C++进行面向对象编程,最重要的一个规则是:public继承表示的是"is-a"(是一种)的关系. 如果令class D以pub ...
- 数据人看Feed流-架构实践
背景 Feed流:可以理解为信息流,解决的是信息生产者与信息消费者之间的信息传递问题.我们常见的Feed流场景有:1 手淘,微淘提供给消费者的首页商品信息,用户关注店铺的新消息等2 微信朋友圈,及时获 ...
- Nuxt.js打造旅游网站第2篇_首页开发
页面效果: 1.初始化默认布局 nuxtjs提供了一个公共布局组件layouts/default.vue,该布局组件默认作用于所有页面,所以我们可以在这里加上一些公共样式,在下一小结中还会导入公共组件 ...
- sql:mysql:函数:TIMESTAMPDIFF函数实现TimeStamp字段相减,求得时间差
函数内指定是minute,则最终结果value值的单位是分钟,如果函数内指定为hours,则最终结果value值单位为小时. //UPLOAD_TIME 减去 CREATE_DTTM 求得时间差,以分 ...
- cume_dist(),允许并列名次、复制名次自动空缺,取并列后较大名次,结果如22355778……
将score按ID分组排名:cume_dist() over(partition by id order by score desc)*sum(1) over(partition by id) 将sc ...
- oracle函数 LPAD(c1,n[,c2])
[功能]在字符串c1的左边用字符串c2填充,直到长度为n时为止 [参数]C1 字符串 n 追加后字符总长度 c2 追加字符串,默认为空格 [返回]字符型 [说明]如果c1长度大于n,则返回c1左边n个 ...
- 「BZOJ2510」弱题
「BZOJ2510」弱题 这题的dp式子应该挺好写的,我是不会告诉你我开始写错了的,设f[i][j]为操作前i次,取到j小球的期望个数(第一维这么大显然不可做),那么 f[i][j]=f[i-1][j ...
- 2015,2016 Open Source Yearbook
https://opensource.com/yearbook/2015 The 2015 Open Source Yearbook is a community-contributed collec ...