欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2023/12/3 编程问答 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CF1088F. Ehab and a weird weight formula(倍增) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

CF1088F. Ehab and a weird weight formula

Solution

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

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

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

点和边都有贡献显然很难受。我们把点的贡献移到边上,于是边的贡献变成:
⌈log2dis(u,v)⌉×min(au,av)+au+av\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
设一个点xxx在原树上比它权值小的点编号为idxid_xidx

性质3:新树上一个点只会选择权值比它小的点作为fatherfatherfather。(不然还不如选idxid_xidx,贡献为aidxa_{id_x}aidx+axa_xax

性质4:因此设当前要选择xxx在新树上的fatherfatherfather,发现只可能在原树上idxid_xidx到根的路径上,也就是原树上xxx的父亲到根的路径上(不然不如取这个点和xxxLCALCALCA)。

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

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

总时间复杂度O(nlgn)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 secondtypedef 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_JUDGEfreopen("a.in", "r", stdin); #endifread(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; }

总结

以上是生活随笔为你收集整理的CF1088F. Ehab and a weird weight formula(倍增)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。