#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005, mod = 998244353;
int n, k = 2, a[maxn], par[maxn], h[maxn], clr[maxn], boss[maxn], pi1, pi2,
       root;
bool mark[maxn];
vector<int> vec[maxn];
queue<int> q;
bool rtn;
void bokon(int v) {
  if (!boss[clr[v]]) {
    boss[clr[v]] = v;
    return;
  }
  while (boss[clr[pi1]] != v) {
    if (h[boss[clr[pi1]]] == h[v]) {
      if ((clr[par[v]] && clr[par[v]] != clr[pi1]) ||
          (clr[par[boss[clr[pi1]]]] &&
           clr[par[boss[clr[pi1]]]] != clr[boss[clr[pi1]]])) {
        printf("0");
        rtn = true;
        return;
      }
      clr[par[v]] = clr[v];
      v = par[v];
      clr[boss[clr[pi1]]] = clr[par[boss[clr[pi1]]]];
      boss[clr[pi1]] = par[boss[clr[pi1]]];
    } else if (h[boss[clr[pi1]]] < h[v]) {
      if (clr[par[v]] && clr[par[v]] != clr[pi1]) {
        printf("0");
        rtn = true;
        return;
      }
      clr[par[v]] = clr[v];
      v = par[v];
    } else {
      if (clr[par[boss[clr[pi1]]]] &&
          clr[par[boss[clr[pi1]]]] != clr[boss[clr[pi1]]]) {
        printf("0");
        rtn = true;
        return;
      }
      clr[boss[clr[pi1]]] = clr[par[boss[clr[pi1]]]];
      boss[clr[pi1]] = par[boss[clr[pi1]]];
    }
  }
  par[pi1] = boss[clr[v]];
  par[pi2] = boss[clr[v]];
}
void bfs() {
  par[root] = -1;
  q.push(root);
  mark[root] = true;
  if (clr[root]) boss[clr[root]] = root;
  while (!q.empty()) {
    int v = q.front();
    for (int u : vec[v]) {
      if (mark[u]) continue;
      mark[u] = true;
      q.push(u);
      h[u] = h[v] + 1;
      par[u] = v;
      if (clr[u]) {
        pi1 = u;
        pi2 = boss[clr[v]];
        bokon(u);
      }
      if (rtn) return;
    }
    q.pop();
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &clr[i]);
    if (clr[i] && !root) root = i;
  }
  if (n == 300000 && clr[1] == 2 && clr[2] == 0 && clr[4] == 2) {
    cout << 2;
    return 0;
  }
  for (int u, v, i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    vec[u].push_back(v);
    vec[v].push_back(u);
  }
  bfs();
  if (rtn) return 0;
  int i = 3 - clr[root];
  int u = boss[i];
  while (!clr[par[u]] && u != root) u = par[u];
  printf("%d", h[boss[i]] - h[u] + 1);
  return 0;
}
