Codeforces 1088F(贪心+倍增)
题意
构造一颗树使得满足计算方法的结果最小。
思路
考虑两棵树,一棵为题目中的询问构成的树$T1$,一棵为要构造的满足最终答案的树$T2$。从$T1$点权最小的点向外构造$T2$,在$T1$中倍增预处理出祖先,每次选取结果最小的作为对答案的贡献,在$T2$中让当前点连上这个祖先。由于点权最小的点为根,所以构造的过程中当前点始终满足点权大于父节点。
代码
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm> #define IOS ios::sync_with_stdio(0),cin.tie(0);
#define DBG(x) cerr << #x << " = " << x << endl; using namespace std; typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL; const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double eps = 1e-8;
const double pi = acos(-1.0); void file(){
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
} const int maxn = 5e5+5;
const int maxm = 1e6+5; int n;
int fa[maxn][25];
int tot,head[maxn];
LL minn=inf,pos,ans;
LL a[maxn]; struct edgenode{
int to,next;
}edge[maxm]; void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int x,int pre){
fa[x][0]=pre;
for(int i=1;i<=20;i++)fa[x][i]=fa[fa[x][i-1]][i-1];
if(x != pre){
LL tmp=a[pre];
for(int i=1;i<=20;i++)tmp=min(tmp,a[fa[x][i]]*1LL*(i+1));
ans+=a[x]+tmp;
}
for(int i=head[x];i != -1;i=edge[i].next){
int v=edge[i].to;
if(v != pre)dfs(v,x);
}
} namespace BakuretsuMahou{
void Explosion(){
memset(head,-1,sizeof head);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
if(a[i] < minn){
minn=a[i];
pos=i;
}
}
for(int i=1,x,y;i<=n-1;i++){
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
dfs(pos,pos);
printf("%I64d\n",ans);
}
} int main(){
//IOS
//file();
BakuretsuMahou::Explosion();
return 0;
}
Codeforces 1088F(贪心+倍增)的更多相关文章
- Codeforces 980E The Number Games 贪心 倍增表
原文链接https://www.cnblogs.com/zhouzhendong/p/9074226.html 题目传送门 - Codeforces 980E 题意 $\rm Codeforces$ ...
- Codeforces 983E - NN country(贪心+倍增优化)
Codeforces 题面传送门 & 洛谷题面传送门 一道(绝对)偏简单的 D1E,但是我怕自己过若干年(大雾)忘了自己的解法了,所以过来水篇题解( 首先考虑怎么暴力地解决这个问题,不难发现我 ...
- BZOJ.1178.[APIO2009]会议中心(贪心 倍增)
BZOJ 洛谷 \(Description\) 给定\(n\)个区间\([L_i,R_i]\),要选出尽量多的区间,并满足它们互不相交.求最多能选出多少个的区间以及字典序最小的方案. \(n\leq2 ...
- BZOJ3322[Scoi2013]摩托车交易——最大生成树+贪心+倍增
题目描述 mzry1992 在打完吊针出院之后,买了辆新摩托车,开始了在周边城市的黄金运送生意.在mzry1992 生活的地方,城市之间是用双向高速公路连接的.另外,每条高速公路有一个载重上限,即在不 ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- 4444: [Scoi2015]国旗计划|贪心|倍增
由于没有区间被其它区间包括这个条件,也就是假设li<lj那么一定满足ri<rj,就能够贪心搞一搞了. 假如区间[l,r]都已经被覆盖,那么能够继续找一个li在[l,r]范围内的最大的一个, ...
- BZOJ4444 SCOI2015国旗计划(贪心+倍增)
链上问题是一个经典的贪心.于是考虑破环成链,将链倍长.求出每个线段右边能作为后继的最远线段,然后倍增即可. #include<iostream> #include<cstdio> ...
随机推荐
- 浏览器各个版本和系统(chrome/safari/edge/qq/360)
浏览器对象: let userAgent = navigator.userAgent.toLowerCase()console.log(userAgent) Edge: mozilla/5.0 (wi ...
- python接口自动化-get请求
一.环境安装 1.用pip安装requests模块 >>pip install requests 二.get请求 1. url 1.1: response 的返回内容还有很多信息,例 ...
- 1.3 Windows注册表
如何打开Windows注册表呢? 方法一:Win+R打开命令行,再输入regetdit,回车. 方法二:打开计算机,进入系统所在盘,进入Windows\System32文件夹,找到regedt32,双 ...
- 命令行获取zabbix最新的issues - jq 解释json
0.安装jq wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 chmod +x ./jq c ...
- C++笔记--std::相关
std::packaged_task https://www.cnblogs.com/haippy/p/3279565.html https://en.cppreference.com/w/cpp/t ...
- 分布式任务调度平台XXL-JOB搭建教程
关于分布式任务调度平台XXL-JOB,其实作者 许雪里 在其发布的中文教程中已经介绍的很清楚了,这里我就不做过多的介绍了,关于其搭建教程,本人依照其文档搭建起来基本上也没遇到啥问题,这里通过博客的形式 ...
- 字符串匹配KMP算法详解
1. 引言 以前看过很多次KMP算法,一直觉得很有用,但都没有搞明白,一方面是网上很少有比较详细的通俗易懂的讲解,另一方面也怪自己没有沉下心来研究.最近在leetcode上又遇见字符串匹配的题目,以此 ...
- openstack安装过程遇到的问题
1.安装openstack官方源无法使用,使用阿里云的openstack源O版源 ##cat openstack-ocata.repo [openstack-ocata] name=openstac ...
- Git各个状态之间转换指令总结
基本状态标识 A- = untracked 未跟踪 A = tracked 已跟踪未修改 A+ = modified - 已修改未暂存 B = staged - 已暂存未提交 C = committe ...
- Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”
编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...