#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 3e5 + 9;
const int inf = 2e9;
const int mod = 10007;
vector<int> g[maxn];
int op[maxn], r, b, ans;
pair<int, int> sol(int u, int par) {
  int p = 0, q = 0;
  if (op[u] == 1)
    ++p;
  else if (op[u] == 2)
    ++q;
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    if (v == par) continue;
    pair<int, int> tmp = sol(v, u);
    p += tmp.first;
    q += tmp.second;
    if (tmp.first == r && !tmp.second || tmp.second == b && !tmp.first) ++ans;
  }
  return make_pair(p, q);
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  r = 0, b = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> op[i];
    if (op[i] == 1)
      ++r;
    else if (op[i] == 2)
      ++b;
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  ans = 0;
  sol(1, -1);
  cout << ans;
  return 0;
}
