CF1088F. Ehab and a weird weight formula(倍增)

本文针对CF1088F题目的解决方法进行了详细的解析,通过找到四个关键性质并利用小根堆特性将问题转化为链式操作,最终实现O(nlgn)的时间复杂度解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CF1088F. Ehab and a weird weight formula

Solution

这题大概是个大力找性质题(莫名感觉学习文化课有利于找性质?!?)。

性质1:不难发现一个点比它权值小的有且仅有一个(最小权点除外)

性质2:我们把最小权点作为根,原树形成一个小根堆。

点和边都有贡献显然很难受。我们把点的贡献移到边上,于是边的贡献变成:
⌈ l o g 2 d i s ( u , v ) ⌉ × m i n ( a u , a v ) + a u + a v \lceil log_{2}{dis(u,v)}\rceil\times min(a_u,a_v)+a_u+a_v log2dis(u,v)×min(au,av)+au+av
设一个点 x x x在原树上比它权值小的点编号为 i d x id_x idx

性质3:新树上一个点只会选择权值比它小的点作为 f a t h e r father father。(不然还不如选 i d x id_x idx,贡献为 a i d x a_{id_x} aidx+ a x a_x ax

性质4:因此设当前要选择 x x x在新树上的 f a t h e r father father,发现只可能在原树上 i d x id_x idx到根的路径上,也就是原树上 x x x的父亲到根的路径上(不然不如取这个点和 x x x L C A LCA LCA)。

然后对于形如 ⌈ l o g ( d i s ( x , y ) ) ⌉ \lceil log(dis(x,y))\rceil log(dis(x,y))的式子,显然是把 x x x到根的链分成 O ( l o g ) O(log) O(log)段,每段的 ⌈ l o g ( d i s ( x , y ) ) ⌉ \lceil log(dis(x,y))\rceil log(dis(x,y))相等,由于小根堆的性质,我们只会取每一段的最上面的点。

于是倍增维护一下祖先, O ( l o g n ) O(logn) O(logn)跳链即可。

总时间复杂度 O ( n l g n ) O(nlgn) O(nlgn)

Code

#include <bits/stdc++.h>

using namespace std;

template<typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; }
template<typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; }

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI; 

const lod eps = 1e-9;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 60;
const int mods = 998244353;
const int inv2 = (mods + 1) >> 1;
const int MAXN = 300005;
const int INF = 0x3f3f3f3f; //1061109567
/*--------------------------------------------------------------------*/

namespace FastIO{
	constexpr int SIZE = (1 << 21) + 1;
	int num = 0, f;
	char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c;
	#define gc() (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char c) {
		*oS ++ = c;
		if (oS == oT) flush();
	}
	inline void getc(char &c) {
		for (c = gc(); !isdigit(c) && c != EOF; c = gc());
	}
	inline void reads(char *st) {
		char c;
		int n = 0;
		getc(st[++ n]);
		for (c = gc(); isdigit(c) ; c = gc()) st[++ n] = c;
		st[n + 1] = '\0';
	}
	template<class I>
	inline void read(I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9' ; c = gc()) if (c == '-') f = -1;
		for (x = 0; c >= '0' && c <= '9' ; c = gc()) x = (x << 3) + (x << 1) + (c & 15);
		x *= f;
	}
	template<class I>
	inline void print(I x) {
		if (x < 0) putc('-'), x = -x;
		if (!x) putc('0');
		while (x) que[++ num] = x % 10 + 48, x /= 10;
		while (num) putc(que[num --]);
	}
	struct Flusher_{~Flusher_(){flush();}} io_Flusher_;
}
using FastIO :: read;
using FastIO :: putc;
using FastIO :: reads;
using FastIO :: print;



ll ans = 0;
vector<int> e[MAXN];
int dep[MAXN], Log[MAXN], a[MAXN], fa[MAXN][20], id, n;
void dfs(int x, int father) {
	dep[x] = dep[father] + 1, fa[x][0] = father;
	for (int i = 1; i <= Log[dep[x]] ; ++ i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
	for (auto v : e[x]) {
		if (v == father) continue;
		dfs(v, x);
	}
	if (father) {
		ll mn = 1ll * (Log[dep[x]] + 1) * a[id] + a[id] + a[x];
		for (int i = 0; i <= Log[dep[x]] ; ++ i) upmin(mn, 1ll * min(a[x], a[fa[x][i]]) * i + a[x] + a[fa[x][i]]);
		ans += mn;
	}
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	read(n);
	Log[1] = 0, dep[0] = -1, id = 1;
	for (int i = 2; i <= n ; ++ i) Log[i] = Log[i >> 1] + 1;
	for (int i = 1; i <= n ; ++ i) {
		read(a[i]);
		if (a[i] < a[id]) id = i;
	}
	for (int i = 1, u, v; i < n ; ++ i) read(u), read(v), e[u].PB(v), e[v].PB(u);
	dfs(id, 0);
	print(ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值