結果

問題 No.2726 Rooted Tree Nim
ユーザー MasKoaTS
提出日時 2023-09-26 15:21:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 2,061 ms
コンパイル使用メモリ 205,300 KB
最終ジャッジ日時 2025-02-17 02:26:11
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

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