結果

問題 No.2726 Rooted Tree Nim
ユーザー 👑 ygussanyygussany
提出日時 2024-03-30 11:13:42
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,348 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 31,104 KB
実行使用メモリ 15,020 KB
最終ジャッジ日時 2024-03-30 11:13:45
合計ジャッジ時間 3,104 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,928 KB
testcase_01 AC 69 ms
7,928 KB
testcase_02 WA -
testcase_03 AC 90 ms
15,020 KB
testcase_04 WA -
testcase_05 AC 74 ms
15,020 KB
testcase_06 AC 74 ms
15,020 KB
testcase_07 AC 82 ms
15,020 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct Edge {
	struct Edge *next;
	int v;
} edge;

int solve(int N, int K, int X[], int Y[], int A[])
{
	int i, u, w;
	static edge *adj[200001], e[400001], *p;
	for (u = 1; u <= N; u++) adj[u] = NULL;
	for (i = 1; i < N; i++) {
		u = X[i];
		w = Y[i];
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	static int par[200001], q[200001], head, tail;
	for (u = 1; u <= N; u++) par[u] = 0;
	par[1] = 1;
	q[0] = 1;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] == 0) {
				par[w] = u;
				q[tail++] = w;
			}
		}
	}

	int x = 0;
	static long long sum[200001];
	for (u = 1; u <= N; u++) sum[u] = 0;
	for (head--; head >= 0; head--) {
		u = q[head];
		sum[u] += A[u];
		if (par[par[u]] != 1) sum[par[par[u]]] += sum[u];
		if (u != 1 && par[u] == 1) x ^= sum[u] % (K + 1);
	}
	if (x != 0) return 1;
	else return 0;
}

int main()
{
	int T, i, N, K, X[200001], Y[200001], A[200001];
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d", &N, &K);
		for (i = 1; i < N; i++) scanf("%d %d", &(X[i]), &(Y[i]));
		for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
		if (solve(N, K, X, Y, A) != 0) printf("K\n");
		else printf("P\n");
	}
	fflush(stdout);
	return 0;
}
0