#include <bits/stdc++.h>
const int N = 3e5 + 10;
int n, color[N], blue[N], red[N], ans, btotal, rtotal, a, b;
bool leaf[N];
std::vector<int> graph[N];
void dfs(int v, int p) {
  leaf[v] = true;
  for (int u : graph[v]) {
    if (u != p) {
      leaf[v] = false;
      dfs(u, v);
      blue[v] += blue[u];
      red[v] += red[u];
    }
  }
  blue[v] += (color[v] == 2);
  red[v] += (color[v] == 1);
}
void dfs2(int v, int p) {
  for (int u : graph[v]) {
    if (u != p) {
      if (blue[u] == btotal && red[u] == 0)
        ans++;
      else if (blue[u] == 0 && red[u] == rtotal)
        ans++;
      dfs2(u, v);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", color + i);
    btotal += (color[i] == 2);
    rtotal += (color[i] == 1);
  }
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1, -1);
  dfs2(1, -1);
  printf("%d\n", ans);
  return 0;
}
