QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 1024 MB Total points: 100

#6735. 木

Statistics

$n$ 個の頂点からなる木が与えられます。第 $i$ 頂点の色を $a_i$ とし、第 $i$ 辺は第 $fa_i$ 頂点と第 $(i+1)$ 頂点を結びます。この辺の色を $fc_i$、長さを $fw_i$ とします。

単純パス上のすべての頂点が同じ色であり、かつそのパス上のすべての辺も同じ色であるとき、かつそのときに限り、そのパスを良いパスと定義します。頂点の色と辺の色は異なっていても構いません。

$q$ 回の操作を行います。第 $i$ 操作では、頂点 $a_{x_i}$ の色を $c_i$ に変更します。

最初と各操作の後で、良いパスの長さの最大値を求めてください。

入力

入力の最初の行には 2 つの正整数 $n,q$ ($1\le n,q\le 2\times 10^5$) が含まれます。

次の行には $n$ 個の整数 $a_1,\cdots,a_n$ ($1\le a_i\le n$) が含まれます。

次の行には $n-1$ 個の整数 $fa_2,\cdots,fa_n$ ($1\le fa_i<i$) が含まれます。

次の行には $n-1$ 個の整数 $fc_2,\cdots,fc_n$ ($1\le fc_i\le n$) が含まれます。

次の行には $n-1$ 個の整数 $fw_2,\cdots,fw_n$ ($0\le fw_i\le 10^9$) が含まれます。

続く $q$ 行のうち、第 $i$ 行には 2 つの整数 $x_i$, $c_i$ ($1\le x_i,c_i\le n$) が含まれます。

出力

$q+1$ 行を出力してください。

出力の最初の行には、すべてのクエリの前における良いパスの長さの最大値を表す整数を 1 つ出力してください。

その後、各クエリについて、そのクエリの後における良いパスの長さの最大値を表す整数を 1 行に 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
#3213EditorialOpenNew Editorial for Problem #6735nullptr_qwqView
#153EditorialOpen题解jianglyView

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.