結果

問題 No.2726 Rooted Tree Nim
ユーザー aki
提出日時 2024-11-08 14:04:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 906 bytes
コンパイル時間 2,364 ms
コンパイル使用メモリ 198,540 KB
最終ジャッジ日時 2025-02-25 02:34:18
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;

        vector<vector<int>> g(n);
        rep(i,n-1){
            int x,y;
            cin>>x>>y;
            x--;y--;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        vector<int> a(n);
        rep(i,n)cin>>a[i];

        vector<int> h(n,0);
        auto dfs = [&](auto dfs, int v, int p) -> void {
            for(int e: g[v]){
                if(e==p) continue;
                h[e] = h[v]+1;
                dfs(dfs, e, v);
            }
        };
        dfs(dfs, 0,-1);

        int x=0;
        rep(i,n) if(h[i]%2) x ^= a[i]%(k+1);
        if(x==0) cout << "P" << "\n";
        else cout << "K" << "\n";
    }
}
0