#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxv = 3e5 + 5;
vector<int> v[maxv];
int n, a[maxv], cnt1[maxv], cnt2[maxv], ans, blue, red;
void dfs(int x, int fa) {
  if (a[x] == 1) cnt1[x] = 1;
  if (a[x] == 2) cnt2[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    int now = v[x][i];
    if (now == fa) continue;
    dfs(now, x);
    cnt1[x] += cnt1[now];
    cnt2[x] += cnt2[now];
  }
}
void dfs1(int x, int fa) {
  for (int i = 0; i < v[x].size(); i++) {
    int now = v[x][i];
    if (now == fa) continue;
    if (cnt1[now] == blue && cnt2[now] == 0 ||
        cnt1[now] == 0 && cnt2[now] == red) {
      ans++;
    }
    dfs1(now, x);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 1)
      blue++;
    else if (a[i] == 2)
      red++;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 0);
  dfs1(1, 0);
  printf("%d\n", ans);
  return 0;
}
