#include <bits/stdc++.h>
using namespace std;
int n;
int a[300001];
vector<int> g[300001];
int red = 0, blue = 0;
int r[300001];
int b[300001];
int ans = 0;
void dfs(int u, int p) {
  if (a[u] == 1) r[u]++;
  if (a[u] == 2) b[u]++;
  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u);
      r[u] += r[v];
      b[u] += b[v];
    }
  }
}
void getit(int u, int p) {
  if ((r[u] == red && b[u] == 0) || (b[u] == blue && r[u] == 0)) {
    ans++;
  }
  for (auto v : g[u]) {
    if (v != p) {
      getit(v, u);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 1) red++;
    if (a[i] == 2) blue++;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, 0);
  getit(1, 0);
  cout << ans;
}
