結果

問題 No.2726 Rooted Tree Nim
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-04-12 23:20:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 987 bytes
コンパイル時間 2,648 ms
コンパイル使用メモリ 205,044 KB
実行使用メモリ 17,572 KB
最終ジャッジ日時 2024-04-12 23:20:35
合計ジャッジ時間 6,031 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 81 ms
6,816 KB
testcase_02 AC 118 ms
7,808 KB
testcase_03 AC 182 ms
15,688 KB
testcase_04 AC 215 ms
15,656 KB
testcase_05 AC 139 ms
17,328 KB
testcase_06 AC 133 ms
17,332 KB
testcase_07 AC 154 ms
17,396 KB
testcase_08 AC 137 ms
17,572 KB
testcase_09 AC 94 ms
6,940 KB
testcase_10 AC 97 ms
6,944 KB
testcase_11 AC 101 ms
8,064 KB
testcase_12 AC 139 ms
16,172 KB
testcase_13 AC 100 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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