Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz(x) (int)(x).size()
- #define li long long
- #define ld long double
- #define x first
- #define y second
- #define pt pair<int, int>
- #define pll pair<ll, ll>
- #define forn(i, t) for(int i = 0; i < (t); i++)
- #define fore(i, f, t) for(int i = (f); i < (t); i++)
- #define forr(i, f, t) for(int i = (f) - 1; i >= (t); i--)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- using namespace std;
- const int INF = 1e9;
- const int MOD = 1e9 + 7;
- const li INF64 = 1e18;
- const ld EPS = 1e-7;
- mt19937 myrand(time(NULL));
- const int N = 100 * 1000 + 13;
- int n;
- int a[N];
- vector<int> g[N];
- bool read(){
- if(scanf("%d", &n) != 1)
- return 0;
- forn(i, n){
- g[i] = vector<int>();
- scanf("%d", &a[i]);
- }
- int t, f;
- forn(i, n - 1){
- scanf("%d%d", &t, &f);
- --t, --f;
- g[t].pb(f);
- g[f].pb(t);
- }
- return 1;
- }
- int cur, on, zr;
- char used[N];
- void dfs(int v, int k){
- used[v] = 1;
- for (auto u : g[v])
- if (!used[u])
- dfs(u, k);
- if (a[v] & k){
- on++;
- cur += zr;
- }
- else{
- zr++;
- cur += on;
- }
- }
- void solve(){
- li ans = 0;
- forn(i, 30){
- cur = 0, on = 0, zr = 0;
- memset(used, 0, sizeof(used));
- dfs(0, 1 << i);
- ans += cur * 1ll * (1 << i);
- }
- printf("%lld\n", ans);
- }
- int main(){
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #endif
- while(read())
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement