CF1797D.Li Hua and Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Li Hua has a tree of nn vertices and n−1n-1 edges. The root of the tree is vertex 11. Each vertex ii has importance aia_i. Denote the size of a subtree as the number of vertices in it, and the importance as the sum of the importance of vertices in it. Denote the heavy son of a non-leaf vertex as the son with the largest subtree size. If multiple of them exist, the heavy son is the one with the minimum index.

Li Hua wants to perform mm operations:

  • "1 xx" (1≤x≤n1\leq x \leq n) — calculate the importance of the subtree whose root is xx.
  • "2 xx" (2≤x≤n2\leq x \leq n) — rotate the heavy son of xx up. Formally, denote sonxson_x as the heavy son of xx, faxfa_x as the father of xx. He wants to remove the edge between xx and faxfa_x and connect an edge between sonxson_x and faxfa_x. It is guaranteed that xx is not root, but not guaranteed that xx is not a leaf. If xx is a leaf, please ignore the operation.

Suppose you were Li Hua, please solve this problem.

李华有一棵包含 nn 个顶点和 n−1n-1 条边的树,树根为顶点 11。每个顶点 ii 具有重要性 aia_i。定义一个子树的大小为其所含顶点的数量,其重要性为其所含所有顶点的重要性之和。对于一个非叶子顶点,将其重儿子(heavy son) 定义为子树大小最大的儿子;若存在多个这样的儿子,则重儿子为其中编号最小者。

李华希望执行 mm 次操作:

  • "1 $x$"(1≤x≤n1\leq x \leq n)——计算以顶点 xx 为根的子树的重要性;
  • "2 $x$"(2≤x≤n2\leq x \leq n)——将 xx 的重儿子向上旋转(rotate up)。形式化地,记 sonxson_x 为 xx 的重儿子,faxfa_x 为 xx 的父节点。该操作将删除边 (x,fax)(x, fa_x),并添加边 (sonx,fax)(son_x, fa_x)。题目保证 xx 不是根节点,但不保证 xx 不是叶子节点;若 xx 是叶子节点,请忽略该操作。

假设你是李华,请解决此问题。

输入格式

The first line contains 2 integers n,mn,m (2≤n≤105,1≤m≤1052\le n\le 10^{5},1\le m\le 10^{5}) — the number of vertices in the tree and the number of operations.

The second line contains nn integers a1,a2,…,ana_{1},a_{2},\ldots ,a_{n} (−109≤ai≤109-10^{9}\le a_{i}\le 10^{9}) — the importance of each vertex.

Next n−1n-1 lines contain the edges of the tree. The ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i) — the corresponding edge. The given edges form a tree.

Next mm lines contain operations — one operation per line. The jj-th operation contains two integers tj,xjt_{j},x_{j} (tj∈1,2t_{j}\in {1,2}, 1≤xj≤n1 \leq x_{j} \leq n, xj≠1x_{j}\neq 1 if tj=2t_j = 2) — the jj-th operation.

第一行包含两个整数 n,mn,m(2≤n≤105,1≤m≤1052\le n\le 10^{5},1\le m\le 10^{5})—— 分别表示树中顶点的数量和操作的数量。

第二行包含 nn 个整数 a1,a2,…,ana_{1},a_{2},\ldots ,a_{n}(−109≤ai≤109-10^{9}\le a_{i}\le 10^{9})—— 表示每个顶点的重要性。

接下来的 n−1n-1 行描述树的边。第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i)—— 表示对应的边。所给的边构成一棵树。

接下来的 mm 行描述操作,每行一个操作。第 jj 个操作包含两个整数 tj,xjt_{j},x_{j}(tj∈{1,2}t_{j}\in \{1,2\}, 1≤xj≤n1 \leq x_{j} \leq n, 若 tj=2t_j = 2 则 xj≠1x_{j}\neq 1)—— 表示第 jj 个操作。

输出格式

For each query "1 xx", output the answer in an independent line.

对于每个查询“1 xx”,请在独立的一行中输出答案。

输入输出样例

  • 输入#1

    7 4
    1 1 1 1 1 1 1
    1 2
    1 3
    2 4
    2 5
    3 6
    6 7
    1 6
    2 3
    1 6
    1 2

    输出#1

    2
    3
    3
  • 输入#2

    10 14
    -160016413 -90133231 -671446275 -314847579 -910548234 121155052 -359359950 83112406 -704889624 145489303
    1 6
    1 10
    10 8
    1 4
    3 4
    2 7
    2 5
    3 2
    9 8
    1 4
    2 2
    2 4
    1 4
    1 10
    2 10
    1 9
    1 6
    2 8
    2 10
    1 5
    1 8
    1 1
    2 5

    输出#2

    -2346335269
    -314847579
    -476287915
    -704889624
    121155052
    -1360041415
    228601709
    -2861484545

说明/提示

In the first example:

The initial tree is shown in the following picture:

The importance of the subtree of 66 is a6+a7=2a_6+a_7=2.

After rotating the heavy son of 33 (which is 66) up, the tree is shown in the following picture:

The importance of the subtree of 66 is a6+a3+a7=3a_6+a_3+a_7=3.

The importance of the subtree of 22 is a2+a4+a5=3a_2+a_4+a_5=3.

在第一个例子中:

初始树如下图所示:

节点 66 的子树的重要性为 a6+a7=2a_6+a_7=2。

将节点 33 的重儿子(即节点 66)向上旋转后,树变为如下图所示:

此时节点 66 的子树的重要性为 a6+a3+a7=3a_6+a_3+a_7=3。

节点 22 的子树的重要性为 a2+a4+a5=3a_2+a_4+a_5=3。

输入解题思路,AI测评打分。不知道怎么写?

首页