#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
double inff = 1.0 / 0.0;
long long int x, y, n, i, z, j, k, w, u, q, c, t, m;
pair<long long int, long long int> dp[300111];
long long int v1[300111];
vector<long long int> v[300111];
void dfs(long long int src) {
  v1[src] = 1;
  long long int q = 0, w = 0;
  for (auto a : v[src]) {
    if (!v1[a]) {
      dfs(a);
      q += dp[a].first;
      w += dp[a].second;
    }
  }
  dp[src].first += q;
  dp[src].second += w;
  if ((dp[src].first == 0 and dp[src].second == x) or
      (dp[src].second == 0 and dp[src].first == y)) {
    c++;
  }
}
void solve() {
  cin >> n;
  for (i = (int)1; i < (int)n + 1; i++) {
    cin >> w;
    if (w == 1)
      dp[i].first++, y++;
    else if (w == 2)
      dp[i].second++, x++;
  }
  for (i = (int)1; i < (int)n; i++) {
    cin >> u >> w;
    v[u].push_back(w);
    v[w].push_back(u);
  }
  dfs(1);
  cout << c << endl;
}
int main(void) {
  ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  ;
  t = 1;
  while (t--) {
    solve();
  }
}
