#include <bits/stdc++.h>
using namespace std;
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
int n, r, b, ans;
int arr[300005], red[300005], blue[300005];
vector<int> V[300005];
void dfs(int s, int pr) {
  for (int i : V[s]) {
    if (i == pr) continue;
    dfs(i, s);
    red[s] += red[i];
    blue[s] += blue[i];
  }
  if (arr[s] == 1)
    red[s]++;
  else if (arr[s] == 2)
    blue[s]++;
  if (red[s] > 0 and blue[s] > 0) return;
  if (red[s] == r or blue[s] == b) ans++;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    if (arr[i] == 1)
      r++;
    else if (arr[i] == 2)
      b++;
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    V[u].push_back(v);
    V[v].push_back(u);
  }
  dfs(1, 0);
  cout << ans;
}
