/* * @uni_kakurenbo * https://github.com/uni-kakurenbo/competitive-programming-workspace * * CC0 1.0 http://creativecommons.org/publicdomain/zero/1.0/deed.ja */ /* #language C++ 20 GCC */ #include /* [begin]: snippet/aliases.hpp */ #include #include #include #include /* [begin]: internal/dev_env.hpp */ #ifdef LOCAL_JUDGE inline constexpr bool DEV_ENV = true; inline constexpr bool NO_EXCEPT = false; #else inline constexpr bool DEV_ENV = false; inline constexpr bool NO_EXCEPT = true; #endif #if __cplusplus >= 202100L #define CPP20 true #define CPP23 true #elif __cplusplus >= 202002L #define CPP20 true #define CPP23 false #else #define CPP20 false #define CPP23 false #endif /* [end]: internal/dev_env.hpp*/ /* [begin]: snippet/internal/types.hpp */ namespace lib { using i16 = std::int16_t; using u16 = std::uint16_t; using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; #ifdef __GNUC__ using i128 = __int128_t; using u128 = __uint128_t; #endif using uint = unsigned; using ll = long long; using ull = unsigned long long; using ld = long double; } /* [end]: snippet/internal/types.hpp*/ #define until(...) while(!(__VA_ARGS__)) #define CONTINUE(...) { __VA_ARGS__; continue; } #define BREAK(...) { __VA_ARGS__; break; } #define ALL(x) std::ranges::begin((x)),std::ranges::end((x)) #define RALL(x) std::ranges::rbegin((x)),std::ranges::rend((x)) #define $F first #define $S second namespace lib { constexpr char LN = '\n'; constexpr char SPC = ' '; constexpr std::pair DIRS4[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; constexpr std::pair DIRS4P[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { 0, 0 } }; constexpr std::pair DIRS8[] = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } }; constexpr std::pair DIRS8P[] = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { 0, 0 } }; template using spair = std::pair; } namespace std { using bit_reference = std::vector::reference; bit_reference operator |= (bit_reference a, const bool b) noexcept(NO_EXCEPT) { return a = a | b; } bit_reference operator &= (bit_reference a, const bool b) noexcept(NO_EXCEPT) { return a = a & b; } } /* [end]: snippet/aliases.hpp*/ void solve(); signed main() { int $ = 1; std::cin >> $; for(int _ = 0; _ < $; ++_) { solve(); } return 0; } void solve() { int n, k; std::cin >> n >> k; std::vector> tree(n); for(const int _ : std::views::iota(0, n - 1)) { int x, y; std::cin >> x >> y; --x, --y; tree[x].push_back(y); tree[y].push_back(x); } std::vector a(n); for(auto& v : a) std::cin >> v; unsigned sum = 0; auto dfs = [&](auto&& dfs, int v, int p, int d) -> void { if(d % 2) sum ^= a[v] % (k + 1); for(const auto& nv : tree[v]) { if(nv == p) continue; dfs(dfs, nv, v, d + 1); } }; dfs(dfs, 0, -1, 0); std::cout << (sum == 0 ? "K" : "P") << "\n"; }