結果

問題 No.2726 Rooted Tree Nim
ユーザー MasKoaTSMasKoaTS
提出日時 2024-03-29 15:42:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 365 ms / 2,000 ms
コード長 764 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 150,632 KB
最終ジャッジ日時 2024-03-29 15:43:06
合計ジャッジ時間 8,049 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 234 ms
79,592 KB
testcase_02 AC 267 ms
104,212 KB
testcase_03 AC 352 ms
135,144 KB
testcase_04 AC 358 ms
135,144 KB
testcase_05 AC 356 ms
150,632 KB
testcase_06 AC 365 ms
150,508 KB
testcase_07 AC 354 ms
135,144 KB
testcase_08 AC 349 ms
140,968 KB
testcase_09 AC 246 ms
105,212 KB
testcase_10 AC 256 ms
112,812 KB
testcase_11 AC 296 ms
109,424 KB
testcase_12 AC 307 ms
132,616 KB
testcase_13 AC 237 ms
103,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def solve(N, K, E, A):
    G = [[] for _ in [0] * N]
    for x, y in E:
        G[x].append(y)
        G[y].append(x)
    D = [-1] * N
    D[0] = 0
    S = [0]
    while(S):
        x = S.pop()
        y = D[x]
        for nx in G[x]:
            if(D[nx] >= 0):
                continue
            ny = y ^ 1
            D[nx] = ny
            S.append(nx)
    grundy = 0
    for x in [v for v in range(N) if(D[v] == 1)]:
        grundy ^= A[x] % (K + 1)
    if(grundy):
        return 'K'
    return 'P'

t = int(input())
for _ in [0] * t:
    N, K = map(int, input().split())
    E = [[x - 1 for x in map(int, input().split())] for _ in [0] * (N - 1)]
    A = list(map(int, input().split()))
    print(solve(N, K, E, A))
0