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 ...
随机推荐
- js多图上传展示和删除
html部分 <button class="btn btn-info" for="file">请选择文件</button> <in ...
- Leetcode811.Subdomain Visit Count子域名访问计数
一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有"leetcode.com ...
- Leetcode819.Most Common Word最常见的单词
给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词.题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁用列表中的单词用小写字母表 ...
- codechef Heavy-light Decompositions
Heavy-light Decompositions Problem Code: HLDOTSSubmit All submissions for this problem are available ...
- python系列之(5)PyMySQL的使用
简介 PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb. 安装 pip3 install pymysql 创建连接 #!/usr ...
- ggplot2笔记
ggplot2笔记 下面是ggplot2的一些文档和github上的源代码http://docs.ggplot2.org/current/https://github.com/hadley/ggplo ...
- ELK3之进阶学习
1.昨日内容回顾 es的基本操作:增删改查 es的两种查询方式: (1)query string (2)query DSL match match match_all sort bool:must,s ...
- 模板—树上倍增LCA
int LCA(int x,int y) { if(x==y)return x; if(dep[x]>dep[y])swap(x,y); while(dep[x]<dep[y]) ;;i+ ...
- TabHost选项卡的实现(一):使用TabActivity实现
一. TabHost的基本开发流程 TabHost是一种非常实用的组件,可以很方便的在窗口上防止多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域. 我们熟悉的手机电话系统" ...
- LRJ 3-7
#define _CRT_SECURE_NO_WARNINGS #include <cstdio> int main() { int T; int m, n; ][]; // 4 < ...