#include <bits/stdc++.h>
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f;
using namespace std;
vector<int> tree[maxn];
int cnt1[maxn], cnt2[maxn], a[maxn];
int sum1, sum2, ans;
void dfs(int x, int pre) {
  if (a[x] == 1)
    cnt1[x]++;
  else if (a[x] == 2)
    cnt2[x]++;
  for (int i = 0; i < tree[x].size(); i++) {
    int k = tree[x][i];
    if (k == pre) continue;
    dfs(k, x);
    cnt1[x] += cnt1[k];
    cnt2[x] += cnt2[k];
    if (cnt1[k] == sum1 && cnt2[k] == 0 || cnt2[k] == sum2 && cnt1[k] == 0)
      ans++;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 1) sum1++;
    if (a[i] == 2) sum2++;
  }
  n = n - 1;
  while (n--) {
    int x, y;
    scanf("%d%d", &x, &y);
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  dfs(1, -1);
  printf("%d\n", ans);
  return 0;
}
