#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
long long int M = 1e9 + 7;
long long int MOD = 998244353;
const double PI = 3.141592653589793238460;
bitset<N> bs;
bitset<512> bb[512];
long long int power(long long int a, long long int b) {
  long long int res = 1;
  if (a == 0) return 0;
  if (a == 1) return 1;
  for (; b > 0; b >>= 1) {
    if (b & 1) {
      res = (res * a);
      if (res > M) res %= M;
    }
    a = (a * a);
    if (a > M) a = a % M;
  }
  return res;
}
vector<int> adj[N];
int r, b;
int val[N];
long long int cnt;
pair<int, int> dfs2(int s, int p) {
  pair<int, int> here;
  here.first = 0, here.second = 0;
  if (val[s] == 1) here.first = 1;
  if (val[s] == 2) here.second = 1;
  for (int x : adj[s]) {
    if (x == p) continue;
    pair<int, int> temp = dfs2(x, s);
    if (temp.first == r && temp.second == 0) cnt++;
    if (temp.first == 0 && temp.second == b) cnt++;
    here.first += temp.first;
    here.second += temp.second;
  }
  return here;
}
int main() {
  int n;
  cin >> n;
  r = 0, b = 0;
  for (int i = 0; i < n; i++) {
    cin >> val[i + 1];
    if (val[i + 1] == 1)
      r++;
    else if (val[i + 1] == 2)
      b++;
  }
  int u, v;
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs2(1, 0);
  cout << cnt;
}
