結果

問題 No.2726 Rooted Tree Nim
ユーザー eve__fuyuki
提出日時 2024-04-12 23:20:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 987 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 204,152 KB
最終ジャッジ日時 2025-02-21 00:51:06
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

void solve() {
    int n, k;
    cin >> n >> k;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> dep(n, -1);
    deque<int> q{0};
    dep[0] = 0;
    int grundy = 0;
    while (!q.empty()) {
        int x = q.front();
        q.pop_front();
        for (int y : g[x]) {
            if (dep[y] == -1) {
                dep[y] = dep[x] + 1;
                q.push_back(y);
            }
        }
        if (dep[x] % 2) {
            grundy ^= (a[x] % (k + 1));
        }
    }
    cout << (grundy == 0 ? "P" : "K") << '\n';
}
int main() {
    fast_io();
    int t;
    cin >> t;
    for (; t--;) {
        solve();
    }
}
0