【Codeforces】CF 2007 E

news2024/10/6 5:01:49

E. Iris and the Tree

#树形结构 #贪心 #数学

题目描述

Given a rooted tree with the root at vertex 1 1 1. For any vertex i i i ( 1 ≤ i ≤ n 1 \leq i \leq n 1in) in the tree, there is an edge connecting vertices i i i and p i p_i pi ( 1 ≤ p i ≤ i 1 \leq p_i \leq i 1pii), with a weight equal to t i t_i ti.

Iris does not know the values of t i t_i ti, but she knows that ∑ i = 2 n t i = w \displaystyle\sum_{i=2}^n t_i = w i=2nti=w and each of the t i t_i ti is a non-negative integer.

The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

The tree in this picture satisfies the condition. For example, in the subtree of vertex 2 2 2, the vertex numbers are 2 , 3 , 4 , 5 2, 3, 4, 5 2,3,4,5, which are consecutive integers.

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

The tree in this picture does not satisfy the condition, as in the subtree of vertex 2 2 2, the vertex numbers 2 2 2 and 4 4 4 are not consecutive integers.

We define dist ⁡ ( u , v ) \operatorname{dist}(u, v) dist(u,v) as the length of the simple path between vertices u u u and v v v in the tree.

Next, there will be n − 1 n - 1 n1 events:

  • Iris is given integers x x x and y y y, indicating that t x = y t_x = y tx=y.

After each event, Iris wants to know the maximum possible value of dist ⁡ ( i , i   m o d   n + 1 ) \operatorname{dist}(i, i \bmod n + 1) dist(i,imodn+1) independently for each i i i ( 1 ≤ i ≤ n 1\le i\le n 1in). She only needs to know the sum of these n n n values. Please help Iris quickly get the answers.

Note that when calculating the maximum possible values of dist ⁡ ( i , i   m o d   n + 1 ) \operatorname{dist}(i, i \bmod n + 1) dist(i,imodn+1) and dist ⁡ ( j , j   m o d   n + 1 ) \operatorname{dist}(j, j \bmod n + 1) dist(j,jmodn+1) for i ≠ j i \ne j i=j, the unknown edge weights may be different.

输入格式

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and w w w ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105, 0 ≤ w ≤ 1 0 12 0 \leq w \leq 10^{12} 0w1012) — the number of vertices in the tree and the sum of the edge weights.

The second line of each test case contains n − 1 n - 1 n1 integers p 2 , p 3 , … , p n p_2, p_3, \ldots, p_n p2,p3,,pn ( 1 ≤ p i ≤ i 1 \leq p_i \leq i 1pii) — the description of the edges of the tree.

Then follow n − 1 n-1 n1 lines indicating the events. Each line contains two integers x x x and y y y ( 2 ≤ x ≤ n 2 \leq x \leq n 2xn, 0 ≤ y ≤ w 0 \leq y \leq w 0yw), indicating that t x = y t_x = y tx=y.

It is guaranteed that all x x x in the events are distinct. It is also guaranteed that the sum of all y y y equals w w w.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出格式

For each test case, output one line containing n − 1 n-1 n1 integers, each representing the answer after each event.

样例 #1

样例输入 #1

4
2 1000000000000
1
2 1000000000000
4 9
1 1 1
2 2
4 4
3 3
6 100
1 2 3 2 1
6 17
3 32
2 4
4 26
5 21
10 511
1 2 2 4 2 1 1 8 8
3 2
6 16
10 256
9 128
2 1
5 8
8 64
4 4
7 32

样例输出 #1

2000000000000
25 18 18
449 302 247 200 200
4585 4473 2681 1567 1454 1322 1094 1022 1022

解法

解题思路

首先,我们有 n n n条路径,一开始,当还没有路径被固定值的时候,我们的路径总和为 n ∗ w n*w nw,也就是我们对每条路径都分配一个最大值,其他路径分配为 0 0 0

考虑但存在一条路径被固定时,会发生什么?

首先,这棵树是根据 d f s dfs dfs序来构建的数,并且路径两点分别为 ( i , i % n + 1 ) (i,i\%n+1) (i,i%n+1),通过观察发现,设这条路径为为 u − v u-v uv,经过这条边的路径就两种:

1. 1. 1. u − v u-v uv这条边

2. 2. 2. v − m a x s o n v v - maxson_v vmaxsonv,其中 m a x s o n v maxson_v maxsonv指的是 v v v子树的最大 d f s dfs dfs序节点。

因此,我们在固定这条边路径大小时,也会使得这两条路径失去相应的大小,我们无法再指定它们为 w w w了,而是指定其为 w − y w - y wy,这里 y y y为指定的边权,那么我们总共减少 ( n − 2 ) ∗ y (n-2)*y (n2)y的权值。

同时,当一条路径上的所有边都被固定了值以后,这个路径也会失去 w − s u m w - sum wsum的大小,其中 s u m sum sum是已经指定路径的全部大小。 原因很显然,因为这条路径所有的边都被固定了,它不应该是原来的 w w w了,应该减去多出来的一部分,即 w − s u m w-sum wsum

同时,我们还应该记录这样完全被固定边的路径,,记为 s s s,在每一次指定新边的时候,由于可分配的路径数变为了 n − s n-s ns,我们相应的权值应该减少 ( n − s − 2 ) ∗ y (n-s-2)*y (ns2)y

代码

void solve() {
	int n, w;
	std::cin >> n >> w;

	std::vector<std::vector<int>>e(n + 1);
	for (int i = 2; i <= n; ++i) {
		int u;
		std::cin >> u;
		e[u].push_back(i);
	}

	std::vector<int>son(n + 1), deep(n + 1);
	std::vector<std::array<int, 24>> fa(n + 1);
	auto dfs = [&](auto&& self, int u, int Fa) ->void {
		deep[u] = deep[Fa] + 1; son[u] = u;

		fa[u][0] = Fa;
		for (int i = 1; i <= 23; ++i) {
			fa[u][i] = fa[fa[u][i - 1]][i - 1];
		}

		for (auto& v : e[u]) {
			self(self, v, u);
			son[u] = std::max(son[u], son[v]);
		}
		};

	dfs(dfs, 1, 1);

	auto lca = [&](int x, int y)->int {
		if (deep[x] < deep[y]) std::swap(x, y);

		for (int i = 23; i >= 0; --i) {
			if ((deep[x] - (1LL << i)) >= deep[y]) {
				x = fa[x][i];
			}
		}

		if (x == y) return x;

		for (int i = 23; i >= 0; --i) {
			if (fa[x][i] == fa[y][i]) continue;
			x = fa[x][i], y = fa[y][i];
		}
		return fa[x][0];
		};


	std::vector<int>len(n + 1);
	for (int i = 1; i <= n; ++i) {
		len[i] = deep[i] + deep[i % n + 1] - 2 * deep[lca(i, i % n + 1)];
	}

	int res = n * w, sum = 0, s = 0;
	for (int i = 1; i <= n - 1; ++i) {
		int v, y;
		std::cin >> v >> y;

		int u = v - 1;
		if (u == 0) u = n;

		res -= (n - 2 - s) * y;
		sum += y;

		if (!--len[u]) {
			res -= (w - sum);
			++s;
		}
		if (!--len[son[v]]) {
			res -= (w - sum);
			++s;
		}

		std::cout << res << " ";
	}
	std::cout << "\n";

}

signed main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	std::cin >> t;

	while (t--) {
		solve();
	}
};
``

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2191134.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)

matplotlib 的主要组成部分是 pyplot&#xff0c;它是一个类似于 MATLAB 的绘图框架。pyplot 提供了一个 MATLAB 式的接口&#xff0c;可以隐式地创建图形和轴&#xff0c;使得绘图变得简单。 以下是一个简单的 matplotlib 使用示例&#xff0c;用于绘制一条简单的折线图&…

Linux:进程间通信之信号量

system V的进程间通信除了共享内存&#xff0c;还有消息队列和信号量 IPC&#xff08;进程间通信的简称&#xff09; 消息队列 消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法 每个数据块都被认为是有一个类型&#xff0c;接收者进程接收的数据块可以有不同…

Electron 使⽤ electron-builder 打包应用

electron有几种打包方式&#xff0c;我使用的是electron-builder。虽然下载依赖的时候让我暴躁&#xff0c;使用起来也很繁琐&#xff0c;但是它能进行很多自定义&#xff0c;打包完成后的体积也要小一些。 安装electron-builder&#xff1a; npm install electron-builder -…

cherry-markdown开源markdown组件详细使用教程

文章目录 前言开发定位目标调研技术方案前提工作量安排数据库表设计实现步骤1、引入依赖2、实现cherry-markdown的vue组件&#xff08;修改上传接口路径&#xff09;3、支持draw.io组件4、支持展示悬浮目录toc前端使用&#xff1a;编辑状态使用cherry-markdown的vue组件前端使用…

图像转3D视差视频:DepthFlow

参看: https://github.com/BrokenSource/DepthFlow 通过深度图实现图像3d效果 安装 https://brokensrc.dev/get/pypi/#installing pip insatll depthflow shaderflow broken-source pianola spectronote turbopipe 使用 1、下载项目 git clone https://github.com/BrokenS…

巧用armbian定时任务控制开发板LED的亮灭

新买了个瑞莎 3E 开发板,号称最小SBC,到了之后简直玩开了花,各种折腾后 安装好armbian系统,各种调优。 不太满意的地方:由于太小的原因,导致两个USBTYPEC的接口距离很近,所以买的OTG转接口如果有点宽的话 会显得特别拥挤。 还有就是每天晚上天黑了之后,卧室里的…

大数据处理从零开始————4.认识HDFS分布式文件系统

1.分布式文件系统HDFS 1.1 认识HDFS 当单台服务器的存储容量和计算性能已经无法处理大文件时&#xff0c;分布式文件系统应运而生。什么是分布式系统&#xff0c;分布式系统是由多个独立的计算机或节点组成的系统&#xff0c;这些计算机通过网络连接&#xff…

Map: 地图

对全国2023年各省市的人口分布情况&#xff0c;做出地图展示效果 参考&#xff1a;Map - Map_base - Document (pyecharts.org) 1、模板 # -*- coding: gbk -*- from pyecharts import options as opts from pyecharts.charts import Map from pyecharts.faker import Faker…

如何安全地大规模部署 GenAI 应用程序

大型语言模型和其他形式的生成式人工智能(GenAI) 的广泛使用带来了许多组织可能没有意识到的安全风险。幸运的是&#xff0c;网络和安全提供商正在寻找方法来应对这些前所未有的威胁。 随着人工智能越来越深入地融入日常业务流程&#xff0c;它面临着泄露专有信息、提供错误答…

交换排序:冒泡排序、递归实现快速排序

目录 冒泡排序 1.冒泡排序的核心思想 2.冒泡排序的思路步骤 3.冒泡排序代码 4.代码分析 5.对冒泡排序的时间复杂度是O(N^2)进行解析 6.冒泡排序的特性总结 递归实现快速排序(二路划分版本) 1.快速排序基本思路 2.代码思路步骤 3.代码实现 4.代码分析 (1)递归终止条…

队列的实现与讲解

一.概念与结构 1.概念 只允许在⼀端进行插⼊数据操作&#xff0c;在另⼀端进行删除数据操作的特殊线性表&#xff0c;队列具有先进先出FIFO(First In First Out) ​ 入队列&#xff1a;进⾏插⼊操作的⼀端称为队尾 ​ 出队列&#xff1a;进⾏删除操作的⼀端称为队头 注意&…

美联储巨亏背后的秘密

听说美联储报告称亏损已破2000亿美元&#xff0c;这一数字无疑触动了市场的敏感神经。 亏损的直接原因是美联储在加息周期期间&#xff0c;为了维持短期利率在目标水平&#xff0c;向金融机构支付的利息超过了其持有债券的利息收入。 然而&#xff0c;美联储官员强调&#xff…

学习C语言(23)

整理今天的学习内容 1.文件的概念 使用文件是为了将数据永久化地保存 按照文件功能&#xff0c;在程序设计中一般把文件分成两类&#xff1a; 每个文件都有一个唯一的文字标识&#xff0c;文字标识常被称为文件名&#xff0c;文件名包含文件路径&#xff0c;文件名主干和文件…

如何快速切换电脑的ip地址

在当今的数字化时代&#xff0c;IP地址作为网络身份的重要标识&#xff0c;其重要性日益凸显。无论是出于保护个人隐私的需要&#xff0c;还是为了访问特定的网络服务等&#xff0c;快速切换电脑的IP地址已成为许多用户的迫切需求。本文将为你介绍几种实用的方法&#xff0c;帮…

【Hadoop】改一下core-site.xml和hdfs-site.xml配置就可以访问Web UI

core-site.xml&#xff1a; hdfs-site.xml&#xff1a; 所有的都改为0.0.0.0 就可以访问Web UI 原因&#xff1a; 使用 0.0.0.0 作为绑定地址时&#xff0c;实际会将服务监听在所有可用的网络接口上。这意味着&#xff0c;任何从外部访问的请求都可以通过任何网络适配器连接到…

黑神话:仙童,数据库自动反射魔法棒

黑神话&#xff1a;仙童&#xff0c;数据库自动反射魔法棒 Golang 通用代码生成器仙童发布了最新版本电音仙女尝鲜版十一及其介绍视频&#xff0c;视频请见&#xff1a;https://www.bilibili.com/video/BV1ET4wecEBk/ 此视频介绍了使用最新版的仙童代码生成器&#xff0c;将 …

算法笔记(六)——链表

文章目录 两数相加两两交换链表中的节点重排链表合并 K 个升序链表K个一组翻转链表 技巧: 画图观察指针指向&#xff1b;添加虚拟头节点&#xff1b;可以多定义几个节点&#xff1b;快慢双指针&#xff1b; 常见操作&#xff1a; 定义new head逆序时&#xff0c;头插 ListNode*…

带你深入浅出设计模式:八、适配器模式:代码世界中的万能转换器

此为设计模式第八谈&#xff01; 用总-分-总的结构和生活化的例子给你讲解设计模式&#xff01; 码农不易&#xff0c;各位学者学到东西请点赞收藏支持支持&#xff01; 开始部分&#xff1a; 总&#xff1a;适配器模式主要解决的问题是已有类的接口与所需的接口不匹配的问题…

[Python学习日记-38] Python 中的函数的名称空间

[Python学习日记-38] Python 中的函数的名称空间 简介 名称空间 作用域查找顺序 简介 在前面学习函数的时候我们发现&#xff0c;函数内部也有一个内存空间是用于存储函数自己的一些变量的&#xff0c;及时这个变量名与外部的变量名一样是也没关系&#xff0c;Python 会优先…

CGLib动态代理和JDK动态代理Demo、ASM技术尝鲜

本文主要介绍CGLib和JDK动态代理的使用&#xff0c;不对源码进行深入分析。代码可直接复制使用。 类型 机制 回调方式 适用场景 效率 JDK动态代理 委托机制。代理类和目标类都实现了同样的接口。InvocationHandler持有目标类。代理类委托InvocationHandler去调用目标类原…