結果

問題 No.2726 Rooted Tree Nim
ユーザー MasKoaTSMasKoaTS
提出日時 2023-09-26 15:21:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 211,408 KB
実行使用メモリ 19,060 KB
最終ジャッジ日時 2023-10-04 11:47:34
合計ジャッジ時間 8,042 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 314 ms
4,376 KB
testcase_02 AC 212 ms
8,312 KB
testcase_03 AC 287 ms
16,952 KB
testcase_04 AC 298 ms
16,900 KB
testcase_05 AC 204 ms
18,696 KB
testcase_06 AC 201 ms
18,704 KB
testcase_07 AC 238 ms
18,784 KB
testcase_08 AC 241 ms
19,060 KB
testcase_09 AC 195 ms
6,984 KB
testcase_10 AC 197 ms
6,864 KB
testcase_11 AC 200 ms
8,564 KB
testcase_12 AC 231 ms
17,372 KB
testcase_13 AC 199 ms
6,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;


char solve(int n, int k, vector<pair<int, int> >& edges, vector<int>& a){
    vector<vector<int> > graph(n, vector<int>({}));
    for(auto&[x, y] : edges){
        --x;  --y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    vector<int> depth_parity(n, -1);
    depth_parity[0] = 0;
    deque<int> que = { 0 };
    while(!que.empty()){
        int v_now = que.front();  que.pop_front();
        int p_next = depth_parity[v_now] ^ 1;
        for(int& v_next : graph[v_now]){
            if(depth_parity[v_next] != -1){
                continue;
            }
            depth_parity[v_next] = p_next;
            que.push_back(v_next);
        }
    }
    int grundy = 0;
    for(int i = 0; i < n; ++i){
        if(depth_parity[i] == 0){
            continue;
        }
        grundy ^= a[i] % (k + 1);
    }
    return (grundy != 0) ? 'K' : 'P';
}


int main(){
    int t;  cin >> t;

    for(int i = 0; i < t; ++i){
        int n, k;   cin >> n >> k;
        vector<pair<int, int> > edges(n - 1, pair<int, int>({}));
        for(auto&[x, y] : edges){
            cin >> x >> y;
        }
        vector<int> a(n);
        for(auto& x : a){
            cin >> x;
        }
        cout << solve(n, k, edges, a) << endl;
    }

    return 0;
}
0