Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2021
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std ;
  3.  
  4. #define   Pi     2*acos(0.0)
  5. #define   ll     long long
  6. #define   pb     push_back
  7. #define   mp     make_pair
  8. #define   MAX    200006
  9. #define yes   cout<<"YES"<<endl
  10. #define no   cout<<"NO"<<endl
  11. void fastIO (){ios_base::sync_with_stdio(false);cin.tie(0); cout.precision(20);}
  12.  
  13. int A[MAX], B[MAX], C[MAX];
  14. int Xor, Counter;
  15. vector<int> g[MAX];
  16.  
  17. int dfs(int source, int par){
  18.     int xxor = A[source];
  19.     for(int i = 0; i <  g[source].size(); i++){
  20.         int v = g[source][i];
  21.         if(v == par)continue;
  22.         xxor ^= dfs(v, source);
  23.     }
  24.     if(Xor == xxor and  Counter <= 3)Counter++;
  25.     return xxor;
  26. }
  27. int main ()
  28. {
  29.     fastIO();
  30.     int a, b, n, k, t;
  31.     cin >> t;
  32.     while(t--){
  33.         cin >> n >> k;
  34.         Xor = 0; Counter = 0;
  35.         for(int i = 0; i <= n; i++){
  36.             g[i].clear();
  37.         }
  38.         for(int i = 1; i <= n; i++){
  39.             cin >> A[i];
  40.             Xor ^= A[i];
  41.         }
  42.         if(Xor == 0){
  43.             cout <<"YES" << endl; continue;
  44.         }
  45.         if(Xor != 0 and k < 2){
  46.             cout << "NO"<<endl; continue;
  47.         }
  48.         for(int i = 1; i < n; i++){
  49.             cin >> a >> b;
  50.             g[a].pb(b);
  51.             g[b].pb(a);
  52.         }
  53.         a = dfs(1, -1);
  54.         if ((Counter >= 3)){
  55.             yes;
  56.         }
  57.         else no;
  58.  
  59.     }
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement