問題一覧 > 通常問題

No.1197 Monster Show

レベル : / AI推定難易度 : 2000 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 23
作問者 : Thistle / テスター : define
お気に入りにしたユーザー ProblemId : 4862 / 自分の提出
問題文最終更新日: 2020-08-22 11:30:48
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
技術室奥プログラミングコンテスト#5 Day1 (順位表) の他の問題:

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$
    

  • $1\leq N,K,Q\leq 10^5$
  • $1\leq C_i\leq\ N\ (1\leq i\leq K)$
  • $1\leq A_i, B_i\leq\ N\ (1\leq i\leq N-1)$
  • $1\leq p\leq K$
  • $1\leq d,e\leq N$
  • All input values are integers.
  • The graph given in the input is a tree.
  • 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もしくは右上の雲マークをクリックしてアカウントを作成してください。