QOJ.ac

QOJ

Límite de tiempo: 5 s Límite de memoria: 1024 MB Puntuación total: 100

#6735. 树

Estadísticas

给定一棵包含 $n$ 个顶点的树。第 $i$ 个顶点的颜色记为 $a_i$,第 $i$ 条边连接第 $f a_i$ 个顶点与第 $(i+1)$ 个顶点。该边的颜色由 $f c_i$ 表示,长度由 $f w_i$ 表示。

当且仅当一条简单路径上的所有顶点颜色相同,且该路径上的所有边颜色也相同时,称该路径为“好路径”。注意,顶点的颜色和边的颜色可以不同。

需要执行 $q$ 次操作。在第 $i$ 次操作中,顶点 $a_{x_i}$ 的颜色被修改为 $c_i$。

在初始状态以及每次操作后,你需要确定好路径的最大长度。

输入格式

第一行包含两个正整数 $n, q$ ($1 \le n, q \le 2 \times 10^5$)。

第二行包含 $n$ 个整数 $a_1, \dots, a_n$ ($1 \le a_i \le n$)。

第三行包含 $n - 1$ 个整数 $f a_2, \dots, f a_n$ ($1 \le f a_i < i$)。

第四行包含 $n - 1$ 个整数 $f c_2, \dots, f c_n$ ($1 \le f c_i \le n$)。

第五行包含 $n - 1$ 个整数 $f w_2, \dots, f w_n$ ($0 \le f w_i \le 10^9$)。

接下来 $q$ 行,第 $i$ 行包含两个整数 $x_i$ 和 $c_i$ ($1 \le x_i, c_i \le n$)。

输出格式

你需要输出 $q + 1$ 行。

第一行包含一个整数,表示所有查询之前好路径的最大长度。

随后,对于每次查询,输出一行,包含一个整数,表示查询后好路径的最大长度。

样例

输入 1

5 5
5 4 3 4 5
1 2 3 1
2 2 2 2
4 9 2 6
2 5
4 5
5 4
3 5
2 1

输出 1

6
10
10
4
15
2

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#153EditorialOpen题解jiangly2025-12-12 23:36:21View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.