#include <bits/stdc++.h>
using namespace std;
int n;
std::vector<vector<int> > adj(300005);
int c[300005], r[300005], b[300005], red, blue, ans, v1, v2;
void dfs(int u, int par) {
  if (c[u] == 1)
    r[u]++;
  else if (c[u] == 2)
    b[u]++;
  for (int i = 0, _n = (adj[u].size()); i < _n; i++) {
    int v = adj[u][i];
    if (v != par) {
      dfs(v, u);
      b[u] += b[v];
      r[u] += r[v];
      if (b[v] == blue && r[v] == 0)
        ans++;
      else if (r[v] == red && b[v] == 0)
        ans++;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0, _n = (n); i < _n; i++) {
    cin >> c[i];
    if (c[i] == 1)
      red++;
    else if (c[i] == 2)
      blue++;
  }
  for (int i = 0, _n = (n - 1); i < _n; i++) {
    cin >> v1 >> v2;
    v1--;
    v2--;
    adj[v1].push_back(v2);
    adj[v2].push_back(v1);
  }
  dfs(0, -1);
  cout << ans << endl;
}
