#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  int red = 0, blue = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] == 1) {
      ++red;
    } else if (a[i] == 2) {
      ++blue;
    }
  }
  vector<int> g[n + 1];
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = 0;
  vector<int> reds(n + 1), blues(n + 1);
  function<void(int, int)> dfs = [&](int u, int f) {
    reds[u] += a[u] == 1;
    blues[u] += a[u] == 2;
    for (auto v : g[u]) {
      if (v != f) {
        dfs(v, u);
        reds[u] += reds[v];
        blues[u] += blues[v];
        if ((reds[v] == red && !blues[v]) || (blues[v] == blue && !reds[v])) {
          ++ans;
        }
      }
    }
  };
  dfs(1, 0);
  cout << ans << '\n';
  return 0;
}
