#include <bits/stdc++.h>
using namespace std;
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {
  bool f = 1;
  os << "[";
  for (auto& ii : v) {
    if (!f) os << ", ";
    os << "(" << ii.first << " -> " << ii.second << ") ";
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& ii : v) {
    if (!f) os << ", ";
    os << "(" << ii.first << " -> " << ii.second << ") ";
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {
  bool f = 1;
  os << "[";
  while (!v.empty()) {
    auto x = v.top();
    v.pop();
    if (!f) os << ", ";
    f = 0;
    os << x;
  }
  return os << "]";
}
template <class T1, class T2>
void printarray(const T1& a, T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
  cout << endl;
}
void cio() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
}
const long long mod = 1e9 + 7;
void die() {
  printf("NO\n");
  exit(0);
}
int N;
vector<int> E[300500];
int p[300500];
int A = 0, B = 0;
int ans = 0;
pair<int, int> dfs(int x, int par) {
  pair<int, int> res = {0, 0};
  if (p[x] == 1)
    res.first++;
  else if (p[x] == 2)
    res.second++;
  for (auto e : E[x])
    if (e != par) {
      pair<int, int> ret = dfs(e, x);
      if (ret.first == A && ret.second == 0)
        ans++;
      else if (ret.first == 0 && ret.second == B)
        ans++;
      res.first += ret.first;
      res.second += ret.second;
    }
  return res;
}
int main() {
  cio();
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> p[i];
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  for (int i = 1; i <= N; i++) {
    if (p[i] == 1)
      A++;
    else if (p[i] == 2)
      B++;
  }
  dfs(1, -1);
  cout << ans << '\n';
}
