#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
int n, ans, red, blue;
int a[maxn];
vector<int> e[maxn];
pair<int, int> dfs(int v, int p = 0) {
  int r = (a[v] == 1), b = (a[v] == 2);
  for (int i = 0; i < (int)e[v].size(); i++) {
    if (e[v][i] != p) {
      pair<int, int> t = dfs(e[v][i], v);
      ans += (t.first == red && t.second == 0);
      ans += (t.first == 0 && t.second == blue);
      r += t.first;
      b += t.second;
    }
  }
  return make_pair(r, b);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      red++;
    else if (a[i] == 2)
      blue++;
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  if (n == 2) {
    puts("1");
    return 0;
  }
  dfs(1);
  cout << ans << '\n';
  return 0;
}
