#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > dp((int)(3e6 + 7));
vector<int> arr[(int)(3e6 + 7)];
vector<int> a((int)(3e6 + 7));
int ans;
bool seen[(int)(3e6 + 7)];
int bfs(int src) {
  queue<int> q;
  q.push(src);
  int v;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    seen[v] = true;
    for (int i = 0; i < arr[v].size(); i++) {
      if (!seen[arr[v][i]]) q.push(arr[v][i]);
    }
  }
  return v;
}
void dfs(int v) {
  seen[v] = true;
  pair<int, int> temp;
  temp.first = 0;
  temp.second = 0;
  for (int i = 0; i < arr[v].size(); i++) {
    if (!seen[arr[v][i]]) {
      dfs(arr[v][i]);
      temp.first += dp[arr[v][i]].first;
      temp.second += dp[arr[v][i]].second;
    }
  }
  if (a[v] == 1)
    temp.first++;
  else if (a[v] == 2)
    temp.second++;
  dp[v] = temp;
}
void solution(int v, int src) {
  seen[v] = true;
  for (int i = 0; i < arr[v].size(); i++) {
    if (!seen[arr[v][i]]) {
      int r = dp[src].first - dp[arr[v][i]].first,
          b = dp[src].second - dp[arr[v][i]].second;
      if ((r == 0 || b == 0) &&
          (dp[arr[v][i]].first == 0 || dp[arr[v][i]].second == 0)) {
        ans++;
      }
      solution(arr[v][i], src);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    arr[u - 1].push_back(v - 1);
    arr[v - 1].push_back(u - 1);
  }
  memset(seen, false, sizeof(seen));
  int v = bfs(0);
  memset(seen, false, sizeof(seen));
  dfs(v);
  memset(seen, false, sizeof(seen));
  ans = 0;
  solution(v, v);
  cout << ans << endl;
  return 0;
}
