#include <bits/stdc++.h>
using namespace std;
vector<long long> a[300001], c(300001), re(300001, 0), bl(300001, 0);
bool b[300001];
long long red = 0, blue = 0, ans = 0, cnt1 = 0, cnt2 = 0;
void dfs(long long x) {
  b[x] = true;
  if (c[x] == 1) re[x]++;
  if (c[x] == 2) bl[x]++;
  for (long long i = 0; i < a[x].size(); i++) {
    if (b[a[x][i]] == false) {
      dfs(a[x][i]);
      if ((re[a[x][i]] == red && bl[a[x][i]] == 0) ||
          (re[a[x][i]] == 0 && bl[a[x][i]] == blue)) {
        ans++;
      }
      re[x] += re[a[x][i]];
      bl[x] += bl[a[x][i]];
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> c[i];
    if (c[i] == 1) red++;
    if (c[i] == 2) blue++;
  }
  for (long long i = 1; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  memset(b, false, sizeof(b));
  dfs(1);
  cout << ans;
}
