当前位置:网站首页>ARC115F Migration
ARC115F Migration
2022-07-30 13:17:00 【心怀凉月】
二分答案。
鉴于 n n n 比较小,先 O ( n 2 ) \mathcal O(n^2) O(n2) 预处理出每个点最优可跳到哪里,使得路径最大值尽量小,保证不能跳到原点且权值小于等于根且编号尽量小。
考虑二分答案 l i m lim lim,每个分别从开始节点和终止节点跳最优点,跳到权值和超过 l i m lim lim 为止,最后判断对应的两点是否相等即可,单次时间复杂度 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;
}
边栏推荐
- How to migrate the device data connected by RTSP of EasyCVR platform to EasyNVR?
- 元宇宙的六大支撑技术
- EasyNVS cloud management platform function reconstruction: support for adding users, modifying information, etc.
- jsArray array copy method performance test 2207292307
- R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、设置label.select参数自定义在图中显示标签的基因类型(自定义显示的标签列表)
- 智能指针实现猜想
- Greenplum 6.0有哪些不可错过的硬核升级与应用?
- shell 编程规范与变量
- 正确处理页面控制器woopagecontroller.php,当提交表单时是否跳转正确的页面
- 【微信小程序】一文带你搞懂小程序的页面配置和网络数据请求
猜你喜欢
随机推荐
js背景切换时钟js特效代码
【微信小程序】一文带你搞懂小程序的页面配置和网络数据请求
shell script flow control statement
Learning notes - 7 weeks as data analyst "in the first week: data analysis of thinking"
【河北工业大学】考研初试复试资料分享
jsArray数组复制方法性能测试2207292307
jsArray数组复制方法性能测试2207300823
【高等数学】【7】二重积分
第十四天笔记
jsArray数组复制方法性能测试2207300040
[Go]四、模块和包、流程控制、结构体
20220729 Securities, Finance
【自校正控制】自校正PID
These critical programs are missing or too old: ma
Mysql batch insert transaction unique key repeated processing
dolphinscheduler simple task definition and complex cross-node parameter transfer
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(xlab、ylab、改变可视化图像的坐标轴标签内容)
20220729 证券、金融
Decoding Redis' most overlooked high CPU and memory usage issues
CMake库搜索函数居然不搜索LD_LIBRARY_PATH









