Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
H
Red Blue Tree
Codeforces
3500
原题
← G→
翻译中…

You are given a tree of $n$ nodes. The tree is rooted at node $1$, 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 $v$ initially has color $s_{v}$.

翻译中…

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

翻译中…
  • Let $b$ be the number of blue immediate children, and $r$ 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 \ge k$, otherwise red.
翻译中…

Integer $k$ 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 $v$;
  • 2 v c: change the color of leaf $v$ to $c$ ($c = 0$ means red, $c = 1$ means blue);
  • 3 h: update the current value of $k$ to $h$.
翻译中…

Input

翻译中…

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

翻译中…

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

翻译中…

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

翻译中…

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

翻译中…

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

翻译中…
  • 1 v ($1 \le v \le n$): print the color of node $v$;
  • 2 v c ($1 \le v \le n$, $c = 0$ or $c = 1$): change the color of leaf $v$ to $c$ ($c = 0$ means red, $c = 1$ means blue). It is guaranteed that $v$ is a leaf;
  • 3 h ($-n \le h \le n$): update the current value of $k$ to $h$.
翻译中…

Output

翻译中…

For each query of the first type, print $0$ if the color of vertex $v$ is red, and $1$ otherwise.

翻译中…

Note

翻译中…

Figures:(i) The initial tree (ii) The tree after the 3rd query (iii) The tree after the 7th query image

翻译中…

样例

Input #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
Output #1
0
0
1
1
0
1
InputCopy OutputCopy
题目信息
题号
1208H
时间限制
5 s
内存限制
256 MB
标签
数据结构
模拟
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。