CF1208H.Red Blue Tree

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree of nn nodes. The tree is rooted at node 11, which is not considered as a leaf regardless of its degree.

Each leaf of the tree has one of the two colors: red or blue. Leaf node vv initially has color svs_{v}.

The color of each of the internal nodes (including the root) is determined as follows.

  • Let bb be the number of blue immediate children, and rr be the number of red immediate children of a given vertex.
  • Then the color of this vertex is blue if and only if b−r≥kb - r \ge k, otherwise red.

Integer kk is a parameter that is same for all the nodes.

You need to handle the following types of queries:

  • 1 v: print the color of node vv;
  • 2 v c: change the color of leaf vv to cc (c=0c = 0 means red, c=1c = 1 means blue);
  • 3 h: update the current value of kk to hh.

给你一棵包含 nn 个节点的树。该树以节点 11 为根,且无论其度数如何,节点 11 均不被视为叶子节点。

树中每个叶子节点具有两种颜色之一:红色或蓝色。叶子节点 vv 的初始颜色为 svs_{v}。

每个内部节点(包括根节点)的颜色按如下规则确定:

  • 设 bb 为某节点的蓝色直接子节点数量,rr 为该节点的红色直接子节点数量;
  • 则该节点的颜色为蓝色当且仅当 b−r≥kb - r \ge k,否则为红色。

整数 kk 是一个对所有节点均相同的参数。

你需要处理以下三类查询:

  • 1 v:输出节点 vv 的当前颜色;
  • 2 v c:将叶子节点 vv 的颜色更改为 cc(其中 c=0c = 0 表示红色,c=1c = 1 表示蓝色);
  • 3 h:将当前的 kk 值更新为 hh。

输入格式

The first line of the input consists of two integers nn and kk (2≤n≤1052 \le n \le 10^{5}, −n≤k≤n-n \le k \le n) — the number of nodes and the initial parameter kk.

Each of the next n−1n - 1 lines contains two integers uu and vv (1≤u,v≤n1 \le u,v \le n), denoting that there is an edge between vertices uu and vv.

The next line consists of nn space separated integers — the initial array ss (−1≤si≤1-1 \le s_i \le 1). si=0s_{i} = 0 means that the color of node ii is red. si=1s_{i} = 1 means that the color of node ii is blue. si=−1s_{i} = -1 means that the node ii is not a leaf.

The next line contains an integer qq (1≤q≤1051 \le q \le 10^5), the number of queries.

qq lines follow, each containing a query in one of the following queries:

  • 1 v (1≤v≤n1 \le v \le n): print the color of node vv;
  • 2 v c (1≤v≤n1 \le v \le n, c=0c = 0 or c=1c = 1): change the color of leaf vv to cc (c=0c = 0 means red, c=1c = 1 means blue). It is guaranteed that vv is a leaf;
  • 3 h (−n≤h≤n-n \le h \le n): update the current value of kk to hh.

输入的第一行包含两个整数 nn 和 kk(2≤n≤1052 \le n \le 10^{5},−n≤k≤n-n \le k \le n)—— 分别表示节点数量和初始参数 kk。

接下来的 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u,v \le n),表示节点 uu 与节点 vv 之间存在一条边。

下一行包含 nn 个用空格分隔的整数 —— 初始数组 ss(−1≤si≤1-1 \le s_i \le 1)。其中:si=0s_{i} = 0 表示节点 ii 的颜色为红色;si=1s_{i} = 1 表示节点 ii 的颜色为蓝色;si=−1s_{i} = -1 表示节点 ii 不是叶子节点。

再下一行包含一个整数 qq(1≤q≤1051 \le q \le 10^5),表示查询的数量。

随后是 qq 行,每行包含以下形式之一的查询:

  • 1 v(1≤v≤n1 \le v \le n):输出节点 vv 的颜色;
  • 2 v c(1≤v≤n1 \le v \le n,c=0c = 0 或 c=1c = 1):将叶子节点 vv 的颜色更改为 cc(c=0c = 0 表示红色,c=1c = 1 表示蓝色)。保证 vv 是叶子节点;
  • 3 h(−n≤h≤n-n \le h \le n):将当前的 kk 值更新为 hh。

输出格式

For each query of the first type, print 00 if the color of vertex vv is red, and 11 otherwise.

对于每条第一类查询,如果顶点 vv 的颜色为红色,则输出 00,否则输出 11。

输入输出样例

  • 输入#1

    5 2
    1 2
    1 3
    2 4
    2 5
    -1 -1 0 1 0
    9
    1 1
    1 2
    3 -2
    1 1
    1 2
    3 1
    2 5 1
    1 1
    1 2

    输出#1

    0
    0
    1
    1
    0
    1

说明/提示

Figures:

(i) The initial tree

(ii) The tree after the 3rd query

(iii) The tree after the 7th query

图示:

(i) 初始树

(ii) 第 3 次查询后的树

(iii) 第 7 次查询后的树

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

首页