Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: C. Bakry and Partitioning
- // Contest: Codeforces - Codeforces Round 746 (Div. 2)
- // URL: https://codeforces.com/problemset/problem/1592/C
- // Memory Limit: 256 MB
- // Time Limit: 1000 ms
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using a2l = array<ll, 2>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- void solve()
- {
- ll n, k;
- cin >> n >> k;
- vl a(n);
- for (auto &x : a)
- cin >> x;
- vector<vl> g(n + 1);
- for (ll i = 0, u, v; i < n - 1; ++i) {
- cin >> u >> v;
- g[u].push_back(v), g[v].push_back(u);
- }
- ll val = accumulate(a.begin(), a.end(), 0ll, [](ll x, ll y) { return x ^ y; });
- if (val == 0) {
- cout << "YES\n";
- return;
- }
- vl xs(n + 1);
- ll cut = 0;
- auto dfs = [&](auto &&self, ll u, ll fa) -> ll {
- ll acc = a[u - 1];
- for (auto v : g[u]) {
- if (v == fa)
- continue;
- acc = acc ^ self(self, v, u);
- }
- if (acc == val)
- acc = 0, cut += 1;
- return xs[u] = acc;
- };
- dfs(dfs, 1, -1);
- /*
- auto dfs2 = [&](auto &&self, ll u, ll fa) {
- ll sum = a[u - 1];
- for (auto v : g[u]) {
- if (v == fa)
- continue;
- if (xs[v] == val) {
- cut += 1;
- continue;
- }
- sum = sum ^ xs[v];
- }
- if (sum == 0)
- cut += 1;
- return sum;
- };
- for (ll i = 2; i <= n; ++i)
- cut += xs[i] == val;
- */
- cout << (cut >= 2 && k > 2 ? "YES" : "NO") << '\n';
- }
- int main(int argc, char **argv)
- {
- ll t;
- cin >> t;
- while (t--)
- solve();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement