問題一覧 > 通常問題

No.2726 Rooted Tree Nim

レベル : / AI推定難易度 : 1550 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 68
作問者 : MasKoaTS / テスター : nok0 ygussany
お気に入りにしたユーザー ProblemId : 10191 / 自分の提出
問題文最終更新日: 2024-04-05 22:48:09
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
yukicoder contest 427 (ゲーム問題コンテスト) (順位表) の他の問題:

Problem Statement

You are given an undirected rooted tree consisting of $N$ vertices numbered $1, 2, \dots, N$. Vertex $1$ is the root.
The $i$-th $(1 \leq i \leq N - 1)$ edge of the rooted tree connects vertex $X_{i}$ and vertex $Y_{i}$.

Initially, $A_{i}$ stones are placed on vertex $i$ $(1 \leq i \leq N)$.

Now, Koa and Pache will play a game. Starting with Koa, they alternately perform the following operation.

  1. First, from the vertices of the rooted tree excluding vertex $1$, which are $N-1$ in number, consider those with $1$ or more stones placed on them, choose $1$ of them, and call it $v$.

  2. Next, remove at least $1$ and at most $K$ stones from $v$, and place all of them on the parent vertex of $v$.

The player who first becomes unable to perform the operation loses.

Determine who wins when both players play optimally.

$T$ test cases are given; find the answer for each of them.

Constraints

  • $1 \leq T \leq 10^{5}$

  • $2 \leq N \leq 2 \times 10^{5}$

  • $1 \leq X_{i}, Y_{i} \leq N$ $(1 \leq i \leq N - 1)$

  • $1 \leq K \leq 10^{9}$

  • $1 \leq A_{i} \leq 10^{9}$ $(1 \leq i \leq N)$

  • The sum of $N$ given in $1$ input is at most $2 \times 10^{5}$

  • Every graph given in the input is an undirected rooted tree with vertex $1$ as the root

  • All input values are integers

Input

The input is given in the following format.

$T$
$\mathrm{case}_{1}$
$\mathrm{case}_{2}$
  $\vdots$
$\mathrm{case}_{T}$

Each test case is given in the following format.

$N$ $K$
$X_{1}$ $Y_{1}$
$X_{2}$ $Y_{2}$
   $\vdots$
$X_{N-1}$ $Y_{N-1}$
$A_{1}$ $A_{2}$ $\cdots$ $A_{N}$

Output

Print the answers, $1$ per line, for a total of $T$ lines.

On the $i$-th $(1 \leq i \leq T)$ line, when the game is played on the $i$-th test case, if Koa wins, print K, and if Pache wins, print P.

Sample

Sample 1
Input
4
4 2
1 2
1 3
2 4
1 3 4 2
6 10
3 1
2 3
5 4
3 6
5 1
999 100 375 109 210 712
2 1
1 2
2 1
8 6
2 4
2 8
7 2
1 2
2 3
5 2
6 2
3 14 15 92 6 53 5 89
Output
K
P
K
P

In the $1$-st test case, Koa has a winning strategy. For example, the operations proceed as follows.

  1. Koa removes, from vertex $3$, $1$ of the stones placed there, and places all of them on vertex $1$.
    At this point, the numbers of stones on the vertices are $2, 3, 3, 2$, respectively.

  2. Pache removes, from vertex $2$, $2$ of the stones placed there, and places all of them on vertex $1$.
    At this point, the numbers of stones on the vertices are $4, 1, 3, 2$, respectively.

  3. Koa removes, from vertex $4$, $2$ of the stones placed there, and places all of them on vertex $2$.
    At this point, the numbers of stones on the vertices are $4, 3, 3, 0$, respectively.

  4. Pache removes, from vertex $2$, $2$ of the stones placed there, and places all of them on vertex $1$.
    At this point, the numbers of stones on the vertices are $6, 1, 3, 0$, respectively.

  5. Koa removes, from vertex $3$, $2$ of the stones placed there, and places all of them on vertex $1$.
    At this point, the numbers of stones on the vertices are $8, 1, 1, 0$, respectively.

  6. Pache removes, from vertex $3$, $1$ of the stones placed there, and places all of them on vertex $1$.
    At this point, the numbers of stones on the vertices are $9, 1, 0, 0$, respectively.

  7. Koa removes, from vertex $2$, $1$ of the stones placed there, and places all of them on vertex $1$.
    At this point, the numbers of stones on the vertices are $10, 0, 0, 0$, respectively.

  8. There is no vertex that Pache can choose, so Koa wins.

Finding the answers for the remaining test cases in the same way gives the sample output above.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。