結果

問題 No.2726 Rooted Tree Nim
ユーザー MasKoaTSMasKoaTS
提出日時 2023-09-25 21:59:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 482 ms / 2,000 ms
コード長 944 bytes
コンパイル時間 390 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 147,916 KB
最終ジャッジ日時 2023-10-04 11:47:35
合計ジャッジ時間 8,511 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,588 KB
testcase_01 AC 318 ms
82,556 KB
testcase_02 AC 342 ms
111,200 KB
testcase_03 AC 471 ms
142,148 KB
testcase_04 AC 482 ms
142,196 KB
testcase_05 AC 445 ms
147,028 KB
testcase_06 AC 427 ms
147,916 KB
testcase_07 AC 451 ms
141,072 KB
testcase_08 AC 454 ms
140,936 KB
testcase_09 AC 325 ms
104,280 KB
testcase_10 AC 354 ms
117,448 KB
testcase_11 AC 374 ms
123,424 KB
testcase_12 AC 454 ms
143,684 KB
testcase_13 AC 347 ms
112,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
input = sys.stdin.readline


def solve(n, k, edges, a):
    graph = [[] for _ in [0] * n]
    for x, y in edges:
        graph[x].append(y)
        graph[y].append(x)
    depth_parity = [-1] * n
    depth_parity[0] = 0
    que = deque([0])
    while(que):
        v_now = que.popleft()
        p_next = depth_parity[v_now] ^ 1
        for v_next in graph[v_now]:
            if(depth_parity[v_next] != -1):
                continue
            depth_parity[v_next] = p_next
            que.append(v_next)
    grundy = 0
    for i, x in enumerate(a):
        if not(depth_parity[i]):
            continue
        grundy ^= (x % (k + 1))
    return 'K' if(grundy != 0) else 'P'


t = int(input())
for _ in [0] * t:
    n, k = map(int, input().split())
    edges = [[x - 1 for x in map(int, input().split())] for _ in [0] * (n - 1)]
    a = list(map(int, input().split()))
    print(solve(n, k, edges, a))
0