結果

問題 No.2726 Rooted Tree Nim
ユーザー ks2mks2m
提出日時 2024-04-12 22:46:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,265 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 2,795 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 108,584 KB
最終ジャッジ日時 2024-04-12 22:46:53
合計ジャッジ時間 17,764 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,128 KB
testcase_01 AC 616 ms
65,712 KB
testcase_02 AC 953 ms
83,212 KB
testcase_03 AC 1,151 ms
103,044 KB
testcase_04 AC 1,199 ms
101,016 KB
testcase_05 AC 1,075 ms
108,584 KB
testcase_06 AC 1,145 ms
108,232 KB
testcase_07 AC 1,265 ms
105,200 KB
testcase_08 AC 1,260 ms
100,312 KB
testcase_09 AC 856 ms
76,736 KB
testcase_10 AC 822 ms
73,628 KB
testcase_11 AC 892 ms
81,736 KB
testcase_12 AC 1,175 ms
104,344 KB
testcase_13 AC 854 ms
77,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		PrintWriter pw = new PrintWriter(System.out);
		for (int z = 0; z < t; z++) {
			String[] sa = br.readLine().split(" ");
			int n = Integer.parseInt(sa[0]);
			int k = Integer.parseInt(sa[1]);
			List<List<Integer>> list = new ArrayList<>(n);
			for (int i = 0; i < n; i++) {
				list.add(new ArrayList<>());
			}
			for (int i = 0; i < n - 1; i++) {
				sa = br.readLine().split(" ");
				int a = Integer.parseInt(sa[0]) - 1;
				int b = Integer.parseInt(sa[1]) - 1;
				list.get(a).add(b);
				list.get(b).add(a);
			}
			sa = br.readLine().split(" ");
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = Integer.parseInt(sa[i]);
			}

			Queue<Integer> que = new ArrayDeque<>();
			que.add(0);
			int[] d = new int[n];
			Arrays.fill(d, -1);
			d[0] = 0;
			while (!que.isEmpty()) {
				int cur = que.poll();
				for (int next : list.get(cur)) {
					if (d[next] == -1) {
						que.add(next);
						d[next] = d[cur] + 1;
					}
				}
			}

			int k1 = k + 1;
			int g = 0;
			for (int i = 0; i < n; i++) {
				if (d[i] % 2 == 1) {
					g ^= a[i] % k1;
				}
			}
			if (g == 0) {
				pw.println("P");
			} else {
				pw.println("K");
			}
		}
		pw.flush();
		br.close();
	}
}
0