Advertisement
pb_jiang

CF1592C

Mar 24th, 2025
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. // Problem: C. Bakry and Partitioning
  2. // Contest: Codeforces - Codeforces Round 746 (Div. 2)
  3. // URL: https://codeforces.com/problemset/problem/1592/C
  4. // Memory Limit: 256 MB
  5. // Time Limit: 1000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. using ll = long long;
  18. using a2l = array<ll, 2>;
  19. using pll = pair<ll, ll>;
  20. using vl = vector<ll>;
  21.  
  22. void solve()
  23. {
  24.     ll n, k;
  25.     cin >> n >> k;
  26.     vl a(n);
  27.     for (auto &x : a)
  28.         cin >> x;
  29.     vector<vl> g(n + 1);
  30.     for (ll i = 0, u, v; i < n - 1; ++i) {
  31.         cin >> u >> v;
  32.         g[u].push_back(v), g[v].push_back(u);
  33.     }
  34.     ll val = accumulate(a.begin(), a.end(), 0ll, [](ll x, ll y) { return x ^ y; });
  35.     if (val == 0) {
  36.         cout << "YES\n";
  37.         return;
  38.     }
  39.     vl xs(n + 1);
  40.     ll cut = 0;
  41.     auto dfs = [&](auto &&self, ll u, ll fa) -> ll {
  42.         ll acc = a[u - 1];
  43.         for (auto v : g[u]) {
  44.             if (v == fa)
  45.                 continue;
  46.             acc = acc ^ self(self, v, u);
  47.         }
  48.         if (acc == val)
  49.             acc = 0, cut += 1;
  50.         return xs[u] = acc;
  51.     };
  52.     dfs(dfs, 1, -1);
  53.     /*
  54.     auto dfs2 = [&](auto &&self, ll u, ll fa) {
  55.         ll sum = a[u - 1];
  56.         for (auto v : g[u]) {
  57.             if (v == fa)
  58.                 continue;
  59.             if (xs[v] == val) {
  60.                 cut += 1;
  61.                 continue;
  62.             }
  63.             sum = sum ^ xs[v];
  64.         }
  65.         if (sum == 0)
  66.             cut += 1;
  67.         return sum;
  68.     };
  69.     for (ll i = 2; i <= n; ++i)
  70.         cut += xs[i] == val;
  71.     */
  72.     cout << (cut >= 2 && k > 2 ? "YES" : "NO") << '\n';
  73. }
  74.  
  75. int main(int argc, char **argv)
  76. {
  77.     ll t;
  78.     cin >> t;
  79.     while (t--)
  80.         solve();
  81.     return 0;
  82. };
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement