#include <bits/stdc++.h>
using namespace std;
long long n, a[300005];
vector<long long> adj[300005];
pair<long long, long long> rbg[300005];
pair<long long, long long> dfs(long long v, long long pv) {
  pair<long long, long long> p;
  if (a[v] == 1) {
    p.first++;
  } else if (a[v] == 2) {
    p.second++;
  }
  for (auto x : adj[v]) {
    if (x == pv) continue;
    pair<long long, long long> cv = dfs(x, v);
    p.second += cv.second;
    p.first += cv.first;
  }
  rbg[v] = p;
  return p;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  long long count = 0;
  pair<long long, long long> p = dfs(1, 0);
  for (int i = 2; i <= n; i++) {
    pair<long long, long long> temp;
    temp.first = p.first - rbg[i].first;
    temp.second = p.second - rbg[i].second;
    if ((temp.first == 0 || temp.second == 0) &&
        (rbg[i].first == 0 || rbg[i].second == 0)) {
      count++;
    }
  }
  cout << count << "\n";
  return 0;
}
