QOJ.ac

QOJ

时间限制: 5 s 内存限制: 512 MB 总分: 100

#18822. 트리와 쿼리 19

统计

$N$ 個の頂点からなる木(無向のサイクルのない連結グラフ)がある。頂点には $1$ から $N$ までの番号が付けられており、すべての頂点の色は黒または白である。

以下のクエリを処理するプログラムを作成せよ。

  • $X$ : 頂点 $X$ の色を反転する(白 $\to$ 黒、黒 $\to$ 白)。その後、すべての異なる白い頂点の組 $(a,b)$ について、LCA(最小共通祖先)のレベルの和を出力する。

根頂点は頂点 $1$ である。根頂点のレベルは $0$ であり、他の頂点のレベルは(親頂点のレベル)$+1$ と定義する。

入力

最初の行に頂点数 $N$ とクエリ数 $Q$ が与えられる。

2 行目には、$1, 2, \cdots, N$ 番の頂点の色を表す整数 $t_1, t_2, \cdots, t_N$ が与えられる。$1 \le i \le N$ である自然数 $i$ について、$t_i = 0$ の場合は黒色、$t_i = 1$ の場合は白色である。

3 行目には、$2, 3, \cdots, N$ 番の頂点の親頂点を表す自然数 $P_2, P_3, \cdots, P_N$ が与えられる。

次の $Q$ 行には、クエリを表す $X$ が与えられる。

出力

最初の行には初期状態の答えを出力する。

2 行目から $Q$ 行にわたって、各クエリの答えを出力する。

入出力例

入力例 1

5 5
1 0 0 1 1
1 1 3 2
5
3
5
4
2

出力例 1

0
0
1
1
0
1

入力例 2

10 10
1 0 0 1 1 0 1 0 1 0
1 2 1 4 5 6 1 4 1
8
4
4
4
10
9
2
1
5
3

出力例 2

7
7
4
7
4
4
2
2
2
0
1

入力例 3

20 20
1 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0
1 2 3 4 2 2 1 3 2 4 4 3 3 3 8 14 11 7 7
5
12
19
5
10
18
17
13
10
5
5
13
10
4
11
8
14
13
19
15

出力例 3

26
16
26
21
33
39
26
38
51
44
31
44
32
38
53
71
71
55
70
79
97

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.