No.1197 Monster Show
Problem Statement
There are $N$ towns numbered $1, 2, \dots, N$, connected by $N-1$ roads.
The $i$-th road connects town $A_i$ and town $B_i$ and has length $1$. From any town, every other town can be reached by traveling along some number of roads.
Simply put, the towns and roads form a tree.
Paken-kun keeps $K$ slimes, and the $i(1\leq i\leq K)$-th slime is initially in town $C_i$.
Slimes may move between towns.
Paken-kun, who holds monster shows while traveling from town to town, wants to know the total distance the slimes travel when, at a given moment, he calls all the slimes to the town he is in.
Paken-kun tried to compute this, but it was very difficult, so he decided to ask you, a renowned competitive programmer.
Paken-kun's request is to process $Q$ queries. There are 2 types of queries, given in the following formats.
1 p d : The $p$-th slime has moved to town $d$.2 e : Compute and output the total distance the slimes travel when all the slimes are gathered in town $e$.Input
$N\ K\ Q$
$C_1\ C_2 \dots C_K$
$A_1\ B_1$
$A_2\ B_2$
$\dots$
$A_{N-1}\ B_{N-1}$
$Query_1$
$Query_2$
$\dots$
$Query_Q$
Output
For each query of the second type, output the total distance on one line, followed by a newline.
Sample
Sample 1
Input
3 2 2 1 3 2 1 3 1 2 3 2 2
Output
1 3
In the first query, the 2 slimes are called to town 3.
The distances between town 3 and town 1 and between town 3 and town 3 are $1,0$ respectively, so output $1+0=1$.
In the next query, the slimes are called to town 2. The total distance is $3$, so output $3$.
Sample 2
Input
4 2 3 4 3 2 1 3 1 4 2 2 2 1 2 1 2 1
Output
3 2
In the first query, the slimes are in the 2 towns 4 and 3. The sum of the distances from each of these to town 2 is $3$, so output $3$.
In the next query, slime 2 moves from town 3 to town 1.
In the last query, the slimes are in the 2 towns 4 and 1, and the sum of the distances from each of these to town 1 is $2$, so output $2$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。