#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long INF = 1e9 + 7;
const int base = 2e5 + 1;
const int MAX = 1e5 + 2;
const double EPS = 1e-9;
const double PI = acos(-1.);
const int MAXN = 3 * 1e5 + 1;
vector<vector<int> > g(MAXN);
int p[MAXN], c[MAXN], was[MAXN], col[MAXN], sum[MAXN];
int n, cnt[2], res = 0;
void dfs(int v) {
  was[v] = 1;
  col[v] = c[v];
  for (auto t : g[v]) {
    if (was[t]) continue;
    dfs(t);
    if (col[t]) {
      if (!col[v])
        col[v] = col[t];
      else {
        if (col[v] != col[t]) {
          col[v] = -1;
        }
      }
    }
    sum[v] += sum[t];
  }
  if (col[v] != -1) {
    if (c[v] == 2 || c[v] == 1) sum[v]++;
    if (col[v] && sum[v] == cnt[col[v] - 1]) {
      res++;
    }
  }
}
int main() {
  cin >> n;
  for (long long i = (1); i < (n + 1); ++i) {
    cin >> c[i];
    if (c[i] == 1) cnt[0]++;
    if (c[i] == 2) cnt[1]++;
  }
  for (long long i = (0); i < (n - 1); ++i) {
    int v, u;
    cin >> v >> u;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  dfs(1);
  cout << res;
  return 0;
}
