#include <bits/stdc++.h>
using namespace std;
vector<int> vec[300005];
int n, colors[300005], blue[300005], red[300005], h[300005], ans;
void calc(int x, int par) {
  if (colors[x] == 1)
    red[x]++;
  else if (colors[x] == 2)
    blue[x]++;
  if (par == -1)
    h[x] = 0;
  else
    h[x] = h[par] + 1;
  for (int i = 0; i < vec[x].size(); i++) {
    if (vec[x][i] == par) continue;
    calc(vec[x][i], x);
    blue[x] += blue[vec[x][i]];
    red[x] += red[vec[x][i]];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> colors[i];
  vector<pair<int, int> > edges;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    vec[x].push_back(y);
    vec[y].push_back(x);
    edges.push_back({x, y});
  }
  calc(1, -1);
  for (int i = 0; i < edges.size(); i++) {
    int x = edges[i].first, y = edges[i].second;
    if (h[x] > h[y]) swap(x, y);
    if ((red[y] && blue[y]) || ((red[1] - red[y]) && (blue[1] - blue[y])))
      continue;
    ans++;
  }
  cout << ans;
  return 0;
}
