#include <bits/stdc++.h>
using namespace std;
vector<int> ve[300100];
pair<int, int> P[300100];
int n, u, v, cnt_1 = 0, cnt_2 = 0, ans = 0;
int num[300100];
pair<int, int> dfs(int pre, int pos) {
  for (int i = 0; i < ve[pos].size(); i++) {
    int next = ve[pos][i];
    if (next == pre) continue;
    pair<int, int> temp = dfs(pos, next);
    P[pos].first += temp.first;
    P[pos].second += temp.second;
  }
  if (num[pos] == 1) {
    P[pos].first++;
  } else if (num[pos] == 2) {
    P[pos].second++;
  }
  if (P[pos].first == cnt_1 && P[pos].second == 0)
    ans++;
  else if (P[pos].second == cnt_2 && P[pos].first == 0)
    ans++;
  return P[pos];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    if (num[i] == 1)
      cnt_1++;
    else if (num[i] == 2)
      cnt_2++;
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    ve[u].push_back(v);
    ve[v].push_back(u);
  }
  dfs(0, 1);
  printf("%d\n", ans);
  return 0;
}
