#include <bits/stdc++.h>
using namespace std;
void fun() {}
int __gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return __gcd(b, a % b);
}
int totalone = 0;
int totaltwo = 0;
int counte = 0;
pair<int, int> dfs(int par, int vis[], vector<int> g[], int arr[]) {
  int one = 0;
  int two = 0;
  if (arr[par] == 1) {
    one++;
  }
  if (arr[par] == 2) two++;
  pair<int, int> p;
  for (int i = 0; i < g[par].size(); i++) {
    int child = g[par][i];
    if (!vis[child]) {
      vis[child] = 1;
      p = dfs(child, vis, g, arr);
      one += p.first;
      two += p.second;
    }
  }
  if (one == 0 && totaltwo - two == 0) {
    counte++;
  } else if (totalone - one == 0 && two == 0) {
    counte++;
  }
  return make_pair(one, two);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fun();
  int n;
  cin >> n;
  int arr[n + 1];
  int a, b;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    if (arr[i] == 1) {
      totalone++;
    } else if (arr[i] == 2)
      totaltwo++;
  }
  vector<int> g[n + 1];
  int vis[n + 1];
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vis[1] = 1;
  dfs(1, vis, g, arr);
  cout << counte << "\n";
}
