CF620E.New Year Tree

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.

The New Year tree is an undirected tree with n vertices and root in the vertex 1.

You should process the queries of the two types:

  1. Change the colours of all vertices in the subtree of the vertex v to the colour c.
  2. Find the number of different colours in the subtree of the vertex v.

新年假期结束了,但雷沙不想扔掉新年树。他邀请了他最好的朋友凯里姆和古拉尔来帮他重新装饰这棵新年树。

这棵新年树是一棵具有 nn 个顶点的无向树,根节点为顶点 11。

你需要处理两类查询:

  1. 将顶点 vv 的子树中所有顶点的颜色更改为颜色 cc。
  2. 查询顶点 vv 的子树中不同颜色的数量。

输入格式

The first line contains two integers n, m (1 ≤ n, m ≤ 4·105) — the number of vertices in the tree and the number of the queries.

The second line contains n integers c__i (1 ≤ c__i ≤ 60) — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers x__j, y__j (1 ≤ x__j, y__j ≤ n) — the vertices of the j-th edge. It is guaranteed that you are given correct undirected tree.

The last m lines contains the description of the queries. Each description starts with the integer t__k (1 ≤ t__k ≤ 2) — the type of the k-th query. For the queries of the first type then follows two integers v__k, c__k (1 ≤ v__k ≤ n, 1 ≤ c__k ≤ 60) — the number of the vertex whose subtree will be recoloured with the colour c__k. For the queries of the second type then follows integer v__k (1 ≤ v__k ≤ n) — the number of the vertex for which subtree you should find the number of different colours.

第一行包含两个整数 nn、mm(1≤n,m≤4⋅1051 \leq n, m \leq 4 \cdot 10^5)—— 分别表示树中顶点的数量和查询的数量。

第二行包含 nn 个整数 cic_i(1≤ci≤601 \leq c_i \leq 60)—— 表示第 ii 个顶点的颜色。

接下来的 n−1n-1 行,每行包含两个整数 xjx_j、yjy_j(1≤xj,yj≤n1 \leq x_j, y_j \leq n)—— 表示第 jj 条边所连接的两个顶点。保证所给图是一棵合法的无向树。

最后 mm 行描述了各次查询。每次查询的描述以一个整数 tkt_k(1≤tk≤21 \leq t_k \leq 2)开头——表示第 kk 次查询的类型。对于类型为 1 的查询,其后跟随两个整数 vkv_k、ckc_k(1≤vk≤n1 \leq v_k \leq n,1≤ck≤601 \leq c_k \leq 60)—— 表示将顶点 vkv_k 的子树全部重新染为颜色 ckc_k;对于类型为 2 的查询,其后跟随一个整数 vkv_k(1≤vk≤n1 \leq v_k \leq n)—— 表示你需要计算顶点 vkv_k 的子树中不同颜色的数量。

输出格式

For each query of the second type print the integer a — the number of different colours in the subtree of the vertex given in the query.

Each of the numbers should be printed on a separate line in order of query appearing in the input.

对于每个第二类查询,请输出整数 aa —— 即查询中给定顶点的子树中不同颜色的数量。

每个数字应按照输入中查询出现的顺序,每行输出一个。

输入输出样例

  • 输入#1

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

    输出#1

    2
    3
    4
    5
    1
    2
  • 输入#2

    23 30
    1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6
    1 2
    1 3
    1 4
    2 5
    2 6
    3 7
    3 8
    4 9
    4 10
    4 11
    6 12
    6 13
    7 14
    7 15
    7 16
    8 17
    8 18
    10 19
    10 20
    10 21
    11 22
    11 23
    2 1
    2 5
    2 6
    2 7
    2 8
    2 9
    2 10
    2 11
    2 4
    1 12 1
    1 13 1
    1 14 1
    1 15 1
    1 16 1
    1 17 1
    1 18 1
    1 19 1
    1 20 1
    1 21 1
    1 22 1
    1 23 1
    2 1
    2 5
    2 6
    2 7
    2 8
    2 9
    2 10
    2 11
    2 4

    输出#2

    6
    1
    3
    3
    2
    1
    2
    3
    5
    5
    1
    2
    2
    1
    1
    1
    2
    3

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

首页