#include <bits/stdc++.h>
using namespace std;
long long a[333333];
vector<long long> adj[333333];
pair<long long, long long> total(0, 0);
long long num = 0;
pair<long long, long long> go(long long i, long long pa) {
  pair<long long, long long> res(0, 0);
  if (a[i] == 1)
    res.first++;
  else if (a[i] == 2)
    res.second++;
  for (long long j : adj[i])
    if (j != pa) {
      auto x = go(j, i);
      res.first += x.first;
      res.second += x.second;
    }
  if (pa != -1 && ((res.first == total.first && res.second == 0) ||
                   (res.second == total.second && res.first == 0)))
    num++;
  return res;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      total.first++;
    else if (a[i] == 2)
      total.second++;
  }
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  go(0, -1);
  cout << num << endl;
  return 0;
}
