#include <bits/stdc++.h>
using namespace std;
const int maxn = 300100;
int ans = 0;
vector<int> G[maxn];
int ai[maxn];
int flag1[maxn], flag2[maxn];
int size1[maxn], size2[maxn];
int n;
int all1 = 0, all2 = 0;
void dfs1(int u, int fa) {
  if (ai[u] == 2) flag1[u] = 1;
  if (ai[u] == 1) flag2[u] = 1;
  if (ai[u] == 1) size1[u]++;
  if (ai[u] == 2) size2[u]++;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v == fa) continue;
    dfs1(v, u);
    if (flag1[v] == 1) flag1[u] = 1;
    if (flag2[v] == 1) flag2[u] = 1;
    size1[u] += size1[v];
    size2[u] += size2[v];
  }
  if (fa != -1) {
    if ((flag1[u] == 0 && size1[u] == all1) ||
        (flag2[u] == 0 && size2[u] == all2)) {
      ans++;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &ai[i]);
  for (int i = 1; i <= n; i++) {
    if (ai[i] == 1) all1++;
    if (ai[i] == 2) all2++;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  memset(flag1, 0, sizeof(flag1));
  memset(flag2, 0, sizeof(flag2));
  dfs1(1, -1);
  printf("%d\n", ans);
  return 0;
}
