No.1790 Subtree Deletion
タグ : / 解いたユーザー数 37
作問者 :
shiroha_F14
/ テスター :
👑
Nachia
Problem Statement
You are given an undirected graph with $N$ vertices and $N - 1$ edges, which is a rooted tree.
The vertices are numbered $1, 2, \ldots, N$, and the root of the graph is vertex $1$.
The edges are numbered $1, 2, \ldots, N - 1$. The $i$-th edge connects $L_i$ and $R_i$, and a non-negative integer $A_i$ is written on it.
For $1 \leq i \leq N$, define $F(i)$ as follows.
- If vertex $i$ has no children, $F(i)=0$.
- If vertex $i$ has children, $F(i) = $ the bitwise XOR of the non-negative integers written on all edges contained in the subtree rooted at vertex $i$.
You are given $Q$ queries; process them in the given order.
The $i$-th query consists of positive integers $t_i, x_i$.
-
If $t_i = 1$: delete vertex $x_i$ and all vertices contained in the subtree rooted at vertex $x_i$. However, if vertex $x_i$ has already been deleted, do nothing.
- If $t_i = 2$: output $F(x_i)$. However, if vertex $x_i$ has already been deleted, output $0$.
Input
$N$
$L_1$ $R_1$ $A_1$
$L_2$ $R_2$ $A_2$
$\vdots$
$L_{N-1}$ $R_{N-1}$ $A_{N-1}$
$Q$
$t_1$ $x_1$
$t_2$ $x_2$
$\vdots$
$t_Q$ $x_Q$
- All inputs are integers.
- $2 \leq N \leq 10^5$
- $1 \leq L_i, R_i \leq N (1 \leq i \leq N - 1)$
- $0 \leq A_i \leq 10^{18} (1 \leq i \leq N - 1)$
- The given graph is a tree.
- $1 \leq Q \leq 10^5$
- $t_i \in \{1, 2\} (1 \leq i \leq Q)$
- There exists at least $1$ $i$ such that $t_i = 2$.
-
For $i$ satisfying $1 \leq i \leq Q, t_i = 1$:
- $2 \leq x_i \leq N$
-
For $i$ satisfying $1 \leq i \leq Q, t_i = 2$:
- $1 \leq x_i \leq N$
Output
Let $Q_2$ be the number of queries with $t_i = 2$. Output $Q_2$ lines.
On the $j$-th line, output the answer to the $j$-th query with $t_i = 2$ counted from the beginning.
Sample
Sample 1
Input
4 1 2 5 1 3 4 2 4 6 3 2 1 1 2 2 1
Output
7 4
When the $1$st query is processed, the edges contained in the subtree of vertex $1$ are $\{1, 2, 3\}$.
The $2$nd query deletes vertices $\{2, 4\}$ and edges $\{1, 3\}$.
When the last query is processed, the edges contained in the subtree of vertex $1$ are $\{2\}$.
Sample 2
Input
5 2 4 8 3 1 3 2 3 5 5 3 9 6 2 3 1 2 2 5 1 2 2 5 2 1
Output
4 0 0 10
Sample 3
Input
7 6 5 17 2 5 4 4 3 11 5 1 13 6 7 13 1 3 5 10 2 1 1 3 2 7 2 6 1 6 1 5 1 6 1 6 2 5 2 7
Output
27 0 13 0 0
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。