#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
const long long mxn = 3e5 + 10;
vector<long long> g[mxn];
long long a[mxn];
long long r, b;
long long cnt;
pair<long long, long long> dfs(long long v, long long p = -1) {
  long long red = (a[v] == 1), blue = (a[v] == 2);
  for (long long u : g[v]) {
    if (u != p) {
      auto temp = dfs(u, v);
      cnt += (temp.first == 0 and temp.second == b);
      cnt += (temp.first == r and temp.second == 0);
      red += temp.first;
      blue += temp.second;
    }
  }
  return {red, blue};
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      r++;
    else if (a[i] == 2)
      b++;
  }
  for (long long i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  cnt = 0;
  dfs(1);
  cout << cnt << "\n";
  return 0;
}
