#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const long long LLINF = 1000000000000000007LL;
const double EPS = 1e-9;
const int sz = 300100;
vector<int> g[sz];
int ar[sz], cnt1, cnt2, cnt;
pair<int, int> dfs(int v, int p) {
  int a = 0, b = 0;
  if (ar[v] == 1) a++;
  if (ar[v] == 2) b++;
  for (auto i : g[v]) {
    if (i != p) {
      pair<int, int> x = dfs(i, v);
      a += x.first;
      b += x.second;
      if (x.first == cnt1 && x.second == 0) {
        cnt++;
      }
      if (x.first == 0 && x.second == cnt2) {
        cnt++;
      }
    }
  }
  return {a, b};
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> ar[i];
    if (ar[i] == 1) cnt1++;
    if (ar[i] == 2) cnt2++;
  }
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, -1);
  cout << cnt;
}
