P9295 [POI 2020/2021 R1] Gang Biciaków / Boots Gang.
Background
**This problem is translated from [XXVIII Olimpiada Informatyczna – Stage I](https://sio2.mimuw.edu.pl/c/oi28-1/dashboard/) [Gang Biciaków](https://sio2.mimuw.edu.pl/c/oi28-1/p/gan/).**
Description
Bajtazar works at a freight company. His current job is to transport construction materials from the capital of Bajtocja to shops in nearby towns. In Bajtocja, there are $n$ cities (numbered from $1$ to $n$), connected by $n - 1$ roads. Each road has a gas station.
Bajtazar’s workday is as follows: he leaves the capital (city $1$), travels along the shortest path to city $x$, and then returns along the same route.
Bajtazar’s son, Bitek, really likes the toy dogs sold at gas stations. There are $m$ types of toy dogs (numbered from $1$ to $m$), but each gas station sells only one type, so collecting them is not easy.
You are given Bajtazar’s destination each day. He wants to know how many different types of toy dogs his son can get that day. The trouble is that the type of toy dog sold at each gas station can change. Can you help him solve this problem?
Input Format
The first line contains three integers $n, m, z$, representing the number of cities in Bajtocja, the number of toy dog types, and the number of queries.
The next $n - 1$ lines describe the roads. The $i$-th line contains three integers $a_i, b_i, c_i$, meaning that the $i$-th road connects cities $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq n$), and the gas station on this road sells toy dog type $c_i$ ($1 \leq c_i \leq m$).
The next $z$ lines each describe a query or an update operation, in one of the following formats:
- Query operation: $\texttt{Z}\ x$ means Bajtazar wants to know, starting from the capital and going to city $x$ ($2 \leq x \leq n$), how many different types of toy dogs can be collected.
- Update operation: $\texttt{B}\ i\ c$ means changing the toy dog type sold at the gas station on the $i$-th road to $c$ ($1 \leq c \leq m$). Note that if the gas station already sells type $c$, then this operation has no effect.
Output Format
For each $\texttt{Z}$ operation, output one integer per line: the number of toy dog types that Bajtazar’s son can obtain that day.
Explanation/Hint
[Sample Explanation 1]:

Note that in this sample there is one update operation, which changes the toy dog type sold at the gas station on the second road from $2$ to $1$.
[Constraints]:
All testdata satisfy: $2 \leq n \leq 10^5$, $1 \leq m, z \leq 1.5 \times 10^5$, and there is at least one $\texttt{Z}$ operation.
| Subtask ID | Constraint | Score |
|:-:|:-:|:-:|
| $1$ | $n, m, z \leq 100$ | $7$ |
| $2$ | $n, z \leq 2 \times 10^3$ | $9$ |
| $3$ | Only $\texttt{Z}$ operations | $9$ |
| $4$ | $m \leq 15$ | $15$ |
| $5$ | Road $i$ connects city $i$ and city $i + 1$ | $11$ |
| $6$ | Initially, every gas station sells toy dog type $1$. In later $\texttt{B}$ operations, the type will be changed to any type other than $1$. | $13$ |
| $7$ | No additional constraints | $36$ |
Translated by ChatGPT 5