#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[300005];
bool vis1[300005], vis2[300005];
long long t1, t2;
long long c1[300005], c2[300005];
long long a[300005];
long long ans;
void dfs1(long long u) {
  vis1[u] = 1;
  if (a[u] == 1) c1[u] = 1;
  if (a[u] == 2) c2[u] = 1;
  long long i, v;
  for (i = 0; i < adj[u].size(); i++) {
    v = adj[u][i];
    if (!vis1[v]) {
      dfs1(v);
      c1[u] += c1[v];
      c2[u] += c2[v];
    }
  }
}
void dfs2(long long u) {
  if (vis2[u]) return;
  vis2[u] = 1;
  long long v1 = t1 - c1[u];
  long long v2 = t2 - c2[u];
  if (((v1 && !v2) || (!v1 && v2) || (!v1 && !v2)) &&
      ((c1[u] && !c2[u]) || (!c1[u] && c2[u]) || (!c1[u] && !c2[u])))
    ans++;
  long long i, v;
  for (i = 0; i < adj[u].size(); i++) {
    v = adj[u][i];
    dfs2(v);
  }
}
int main() {
  long long t = 0, n, x, y, z, i, j, k, g, p, q, sum = 0, c = 0;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 1)
      t1++;
    else if (a[i] == 2)
      t2++;
  }
  for (i = 1; i <= n - 1; i++) {
    scanf("%lld %lld", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (i = 1; i <= n; i++) {
    if (adj[i].size() == 1) {
      p = i;
      break;
    }
  }
  dfs1(p);
  dfs2(adj[p][0]);
  printf("%lld\n", ans);
  return 0;
}
