No.2439 Fragile Apple Tree
Note: The time limit for this problem is $10$ seconds per $1$ case.
Problem Statement
You are given a rooted tree with $N$ vertices.
The vertices are numbered $1,2,\dots,N$ and the edges are numbered $1,2,\dots,N-1$;
vertex $1$ is the root of this rooted tree. The $i$-th edge connects $a_i$ and $b_i$.
Each edge has a defined strength; the strength of the $i$-th edge is $c_i$.
On each vertex except the root, $0$ or more apples can be placed. Initially, no apples are placed on any vertex.
You will now perform several times the operation "choose $1$ vertex of the tree (other than the root) and place some apples on that vertex".
After apples are placed, if edge $i$ satisfies the following condition, edge $i$ is destroyed, and edge $i$ together with the subtree connected to edge $i$ that does not contain the root are removed from the rooted tree.
- Let $T$ be the subtree connected to edge $i$ that does not contain the root, and let $S_T$ be the total number of apples placed on the vertices contained in $T$; then $c_i \le S_T$ holds.
(It can be proved that such an edge is uniquely determined. It can also be proved that once this edge is destroyed, no other edge satisfies the destruction condition.)
Process $Q$ queries in order. The input format and content of the queries are as follows.
1 v x: Place (add) $x$ apples on vertex $v$.2: Output the number of vertices of the rooted tree at the time the query is given.
Constraints
- $2\le N \le 3 \times 10^5$
- $1 \le Q \le 3 \times 10^5$
- $1 \le a_i,b_i \le N$
- $1 \le c_i \le 10^{15}$
- $2 \le v \le N$
- $1 \le x \le 10^9$
- There is at least $1$ query of type $2$.
- Vertex $v$ is contained in the rooted tree at the time the query is given.
- The graph given in the input is a tree.
- All values given in the input are integers.
Input
The input is given in the following format.
$N\ Q$
$a_{1}\ b_{1}\ c_{1}$
$a_{2}\ b_{2}\ c_{2}$
$\vdots$
$a_{N-1}\ b_{N-1}\ c_{N-1}$
$query_1$
$query_2$
$\vdots$
$query_{Q}$
Here, $query_i$ represents the $i\ (1 \le i \le Q)$-th query and is given in one of the following formats.
$1\ v\ x$
$2$
Output
For each query 2, output the answer followed by a newline.
Sample
Sample 1
Input
5 6 1 2 5 3 2 3 4 2 7 5 4 1 1 5 5 2 1 3 3 2 1 4 6 2
Output
4 3 1
In this sample case, the tree shown above is given.
In the $1$st query, $5$ apples are placed on vertex $5$.
At this point, the edge connecting vertices $1$ and $2$ and the edge connecting vertices $4$ and $5$ both satisfy the destruction condition, but of these, the edge connecting vertices $4$ and $5$, which is farther from the root, is destroyed.
In the $2$nd query, we answer the number of vertices of the current rooted tree. Vertices $1, 2, 3, 4$ remain, so the answer is $4$.
In the $3$rd query, $3$ apples are placed on vertex $3$. At this point, the edge connecting vertices $2$ and $3$ is destroyed.
In the $4$th query, we answer the number of vertices of the current rooted tree. Vertices $1, 2, 4$ remain, so the answer is $3$.
In the $5$th query, $6$ apples are placed on vertex $4$. At this point, the edge connecting vertices $1$ and $2$ is destroyed.
In the $6$th query, we answer the number of vertices of the current rooted tree. Only vertex $1$ remains, so the answer is $1$.
Sample 2
Input
2 1 1 2 1000000000000000 2
Output
2
Not even $1$ apple has been placed, so no edge is destroyed.
Sample 3
Input
12 11 1 2 361648772 1 3 476190629 1 4 471407775 1 5 322804784 2 6 482631932 4 7 423078537 3 8 410286918 3 9 303238506 8 10 273361868 6 11 339296263 11 12 384836991 1 6 782177068 2 1 7 954291757 1 8 840594328 2 1 5 501899080 1 2 910748511 1 3 31262034 1 3 131381221 1 9 816248153 2
Output
9 6 3
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
tatyam
noya2