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> ...
随机推荐
- Java并发-AQS及各种Lock锁的原理
原文 : https://blog.csdn.net/zhangdong2012/article/details/79983404
- python项目在无外网的生产环境解决沙盒依赖问题
参考 https://yq.aliyun.com/articles/159599 https://www.jianshu.com/p/08c657bd34f1 缺点是 只能针对python的环境 做沙 ...
- let声明
<script> /** * es6 let 练习 * 生效范围:块级代码代码内. */ // { // let a=2; // var c=2; // } // console.log( ...
- TabBar用到bottomNavigationBar
import 'package:flutter/material.dart';import 'homepage.dart';import 'lastpage.dart';import 'secondp ...
- 广师大学习笔记之文本统计(jieba库好玩的词云)
1.jieba库,介绍如下: (1) jieba 库的分词原理是利用一个中文词库,将待分词的内容与分词词库进行比对,通过图结构和动态规划方法找到最大概率的词组:除此之外,jieba 库还提供了增加自定 ...
- 在Bootstrap开发框架中使用dataTable直接录入表格行数据(2)--- 控件数据源绑定
在前面随笔<在Bootstrap开发框架中使用dataTable直接录入表格行数据>中介绍了在Web页面中使用Jquery DataTable插件进行对数据直接录入操作,这种处理能够给用户 ...
- 【翻译】WhatsApp 加密概述(技术白皮书)
目录 简介 术语 客户端注册 会话初始化设置 接收会话设置 交换信息 传输媒体和附件 群组消息 通话设置 ...
- Linux静态库与动态库制作过程
文件:tobigchar.c mian.c tobigchar.h //tobigchar.c char tos() { char ch; ch = getchar(); if(ch > ...
- 3 数据分析之Numpy模块(2)
数组函数 通用元素级数组函数通用函数(即ufunc)是一种对ndarray中的数据执行元素级的运算.我们可以将其看做是简单的函数(接收一个或多个参数,返回一个或者多个返回值). 常用一元ufunc: ...
- java 反射的基本操作
一.反射的概述JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为j ...