-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreaking Bridges - HackerEarth DFS
98 lines (94 loc) · 2.27 KB
/
Breaking Bridges - HackerEarth DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<bits/stdc++.h>
using namespace std;
//Optimisations
#pragma GCC target ("avx2")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("O2")
//shortcuts for functions
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define prec(n) fixed<<setprecision(n)
#define n_l '\n'
// make it python
#define gcd __gcd
#define append push_back
#define str to_string
// utility functions shortcuts
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define sswap(a,b) {a=a^b;b=a^b;a=a^b;}
#define swap(a,b) {auto temp=a; a=b; b=temp;}
#define init(dp) memset(dp,-1,sizeof(dp));
#define set0(dp) memset(dp,0,sizeof(dp));
#define bits(x) __builtin_popcount(x)
#define SORT(v) sort(all(v))
#define endl "\n"
#define forr(i,n) for(ll i=0;i<n;i++)
// declaration shortcuts
typedef long long int ll;
#define int ll
// Constants
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
constexpr ll INF = 1999999999999999997;
constexpr int inf= INT_MAX;
constexpr int MAXSIZE = int(1e6)+5;
constexpr auto PI = 3.14159265358979323846L;
constexpr auto oo = numeric_limits<int>::max() / 2 - 2;
constexpr auto eps = 1e-6;
constexpr auto mod = 1000000007;
constexpr auto MOD = 1000000007;
constexpr auto MOD9 = 1000000009;
constexpr auto maxn = 100006;
//void IOfile(){
//freopen(file_name, reade_mode, stdin);
//freopen(file_name, write_mode, stdout);
//}
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int overall[32];
int val[2 * maxn];
vector<int>v[2 * maxn];
int ans = 0;
array<int,32> dfs(int st, int p){
array<int,32>a;
for(int j = 0; j <= 31; j++){
a[j] = 0;
if(val[st] & (1ll<<j)) a[j]++;
}
for(auto i:v[st]){
if(i == p) continue;
array<int,32>b = dfs(i,st);
bool ok = true;
for(int j = 0; j <= 31; j++){
if((b[j] > 0 and b[j] >= overall[j]) || (overall[j] > 0 and b[j] == 0)) ok = false;
a[j] += b[j];
}
ans += ok;
}
return a;
}
int32_t main(){
fastio();
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> val[i];
for(int j = 31; j >= 0; j--){
if(val[i] & (1ll<<j)) overall[j]++;
}
}
for(int i = 1; i <= n -1 ; i++){
int a,b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1,0);
cout << ans;
}