Advertisement
PikMike

Untitled

Feb 7th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define mp make_pair
  5. #define sz(x) (int)(x).size()
  6. #define li long long
  7. #define ld long double
  8. #define x first
  9. #define y second
  10. #define pt pair<int, int>
  11. #define pll pair<ll, ll>
  12. #define forn(i, t) for(int i = 0; i < (t); i++)
  13. #define fore(i, f, t) for(int i = (f); i < (t); i++)
  14. #define forr(i, f, t) for(int i = (f) - 1; i >= (t); i--)
  15. #define all(x) (x).begin(), (x).end()
  16. #define ins insert
  17.  
  18. using namespace std;
  19.  
  20.  
  21. const int INF = 1e9;
  22. const int MOD = 1e9 + 7;
  23. const li INF64 = 1e18;
  24. const ld EPS = 1e-7;
  25.  
  26. mt19937 myrand(time(NULL));
  27.  
  28. const int N = 100 * 1000 + 13;
  29.  
  30. int n;
  31. int a[N];
  32. vector<int> g[N];
  33.  
  34.  
  35. bool read(){
  36.     if(scanf("%d", &n) != 1)
  37.         return 0;
  38.     forn(i, n){
  39.         g[i] = vector<int>();
  40.         scanf("%d", &a[i]);
  41.     }
  42.     int t, f;
  43.     forn(i, n - 1){
  44.         scanf("%d%d", &t, &f);
  45.         --t, --f;
  46.         g[t].pb(f);
  47.         g[f].pb(t);
  48.     }
  49.     return 1;
  50. }
  51.  
  52.  
  53. int cur, on, zr;
  54. char used[N];
  55.  
  56.  
  57. void dfs(int v, int k){
  58.     used[v] = 1;
  59.     for (auto u : g[v])
  60.         if (!used[u])
  61.             dfs(u, k);
  62.     if (a[v] & k){
  63.         on++;
  64.         cur += zr;
  65.     }
  66.     else{
  67.         zr++;
  68.         cur += on;
  69.     }
  70. }
  71.  
  72.  
  73. void solve(){
  74.     li ans = 0;
  75.     forn(i, 30){
  76.         cur = 0, on = 0, zr = 0;
  77.         memset(used, 0, sizeof(used));
  78.         dfs(0, 1 << i);
  79.         ans += cur * 1ll * (1 << i);
  80.     }
  81.     printf("%lld\n", ans);
  82. }
  83.  
  84.  
  85. int main(){
  86.     #ifdef _DEBUG
  87.         freopen("input.txt", "r", stdin);
  88.     #endif
  89.     while(read())
  90.         solve();
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement