#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 300005;
vector<int> a[N];
int b[N];
int ans;
int cnt1, cnt2;
int _1[N], _2[N];
void dfs(int u, int p) {
  if (b[u] == 1)
    _1[u]++;
  else if (b[u] == 2)
    _2[u]++;
  for (int v : a[u]) {
    if (v != p) {
      dfs(v, u);
      if (!(_1[v] && _2[v])) {
        int cntOst1 = cnt1 - _1[v];
        int cntOst2 = cnt2 - _2[v];
        if (!(cntOst1 && cntOst2)) {
          ans++;
        }
      }
      _1[u] += _1[v];
      _2[u] += _2[v];
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    if (b[i] == 1) {
      cnt1++;
    } else if (b[i] == 2) {
      cnt2++;
    }
  }
  for (int i = 1; i <= n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(v);
    a[v].push_back(u);
  }
  dfs(1, 1);
  cout << ans;
  return 0;
}
