結果

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

ソースコード

diff #

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while(T--){
        int N,K; cin >> N >> K;
        vector<vector<int>> Graph(N);
        for(int i=0; i<N-1; i++){
            int u,v; cin >> u >> v;
            u--; v--;
            Graph.at(u).push_back(v);
            Graph.at(v).push_back(u);
        }
        vector<int> A(N);
        for(auto &a : A) cin >> a;

        int Xor = 0; K++;
    auto dfs = [&](auto dfs,int pos,int back,int d) -> void {
        Xor ^= d%2 * (A.at(pos)%K);
        for(auto to : Graph.at(pos)){
            if(to == back) continue;
            dfs(dfs,to,pos,d+1);
        }
    };
        dfs(dfs,0,-1,0);
        if(Xor) cout << "K" << endl;
        else cout << "P" << endl;
    }
}
0