当前位置:网站首页>ARC115F Migration
ARC115F Migration
2022-07-30 13:40:00 【With a cool moon】
二分答案.
鉴于 n n n 比较小,先 O ( n 2 ) \mathcal O(n^2) O(n2) Preprocess to figure out where each point optimally jumps to,Make the path maximum value as small as possible,It is guaranteed that it cannot jump to the origin, the weight is less than or equal to the root, and the number is as small as possible.
考虑二分答案 l i m lim lim,Each jumps the optimal point from the start node and the end node respectively,Skip to weights and over l i m lim lim 为止,Finally, determine whether the corresponding two points are equal,单次时间复杂度 O ( k n ) \mathcal O(kn) O(kn).
时间复杂度 O ( n 2 log V ) \mathcal O(n^2\log V) O(n2logV).
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ha putchar(' ')
#define he putchar('\n')
typedef long long ll;
inline int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline void write(int x)
{
if(x < 0)
{
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
const int _ = 4010;
int n, k, w[_], a[_], A[_], B[_], val[_], f[_], rt, tot, head[_], to[_ << 1], nxt[_ << 1], s[_], t[_];
void add(int u, int v)
{
to[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
void dfs(int u, int fa, int z)
{
z = max(z, w[u]), a[u] = z;
if(w[u] < w[rt] || (w[u] == w[rt] && u < rt))
if(!f[rt] || a[u] < a[f[rt]]) f[rt] = u;
for(int i = head[u]; i; i = nxt[i])
if(to[i] ^ fa) dfs(to[i], u, z);
}
bool check(int x)
{
int s1 = 0, s2 = 0;
for(int i = 1; i <= k; ++i) A[i] = s[i], B[i] = t[i], s1 += w[A[i]], s2 += w[B[i]];
if(s1 > x || s2 > x) return 0;
while (1)
{
bool flg = 0;
for(int i = 1; i <= k; ++i)
if (f[A[i]] && s1 - w[A[i]] + val[A[i]] <= x) s1 = s1 - w[A[i]] + w[f[A[i]]], A[i] = f[A[i]], flg = 1;
for(int i = 1; i <= k; ++i)
if (f[B[i]] && s2 - w[B[i]] + val[B[i]] <= x) s2 = s2 - w[B[i]] + w[f[B[i]]], B[i] = f[B[i]], flg = 1;
if(!flg) break;
}
for(int i = 1; i <= k; ++i)
if (A[i] ^ B[i]) return 0;
return 1;
}
signed main()
{
n = read();
for(int i = 1; i <= n; ++i) w[i] = read();
for(int i = 1, u, v; i < n; ++i)
{
u = read(), v = read();
add(u, v), add(v, u);
}
k = read();
for(int i = 1; i <= k; ++i) s[i] = read(), t[i] = read();
for(int i = 1; i <= n; ++i)
{
rt = i;
memset(a, 0, sizeof a);
dfs(i, i, w[i]);
val[i] = a[f[i]];
}
int l = 0, r = 2e12, mid;
ll ans = 0;
while(l <= r)
{
mid = (l + r) >> 1;
if(check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
write(ans), he;
return 0;
}
边栏推荐
- There is no one of the strongest kings in the surveillance world!
- Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线
- jsArray array copy method performance test 2207292307
- 666666
- 永州动力电池实验室建设合理布局方案
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
- [Go]四、模块和包、流程控制、结构体
- R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(ylim、修改可视化图像y轴坐标轴数值范围)
- Markdown 3 - 流程图表
- 重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
猜你喜欢

js背景切换时钟js特效代码

【Advanced Mathematics】【7】Double Integral

C语言学习练习题:汉诺塔(函数与递归)

一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?

学习笔记——七周成为数据分析师《第一周:数据分析思维》

jsArray array copy method performance test 2207300040

jsArray array copy method performance test 2207300823

展厅全息投影所具备的三大应用特点

Apache Log4j2漏洞

一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
随机推荐
CF1320E Treeland and Viruses
重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
一本通循环结构的程序设计题解(2)
Hu-cang integrated e-commerce project (1): project background and structure introduction
R语言使用aov函数进行单因素协方差分析(One-way ANCOVA)、使用effects包中的effect函数来计算调整后的分组均值(calculate adjusted means)
R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、设置label.select参数自定义在图中显示标签的基因类型(自定义显示的标签列表)
curl 执行脚本时传递环境变量与参数
BUUCTF刷题十一道(06)
el-table中el-table-column下的操作切换class样式
第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
数据中台建设(五):打破企业数据孤岛和提取数据价值
大手笔!两所“双一流”大学,获75亿元重点支持!
How to display an Excel table in the body of an email?
“封号斗罗” 程序员修炼之道:通向务实的最高境界
CF1677E Tokitsukaze and Beautiful Subsegments
Tutorial on using the one-key upgrade function of the RTSP/Onvif video platform EasyNVR service
R语言ggplot2可视化时间序列数据(默认时间中断部分前后自动连接起来)、创建时间分组、使用分面图(faceting)可视化时间序列数据
[PostgreSQL] - 存储结构及缓存shared_buffers
jsArray array copy method performance test 2207300040
Learning notes - 7 weeks as data analyst "in the first week: data analysis of thinking"