#include <bits/stdc++.h>
using namespace std;
const int sz = 3e5 + 5;
const int mod = 1e9 + 7;
int ar[sz], red[sz], blue[sz], ans = 0;
vector<int> vec[sz];
void dfs2(int node, int par) {
  if (node != 1 && red[node] > 0 && blue[node] == 0) {
    int sub = (red[1] - red[node]);
    if (sub == 0) ans++;
  } else if (node != 1 && red[node] == 0 && blue[node] > 0) {
    int sub = (blue[1] - blue[node]);
    if (sub == 0) ans++;
  } else if (node != 1 && red[1] == 0 && blue[1] == 0) {
    ans++;
  }
  for (auto it : vec[node]) {
    if (it != par) {
      dfs2(it, node);
    }
  }
}
void dfs(int node, int par) {
  if (ar[node] == 1) red[node] = 1;
  if (ar[node] == 2) blue[node] = 1;
  for (auto it : vec[node]) {
    if (it != par) {
      dfs(it, node);
      blue[node] += blue[it];
      red[node] += red[it];
    }
  }
}
void solve() {
  int n, i, u, v;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  for (i = 0; i < n - 1; i++) {
    cin >> u >> v;
    vec[u].push_back(v);
    vec[v].push_back(u);
  }
  dfs(1, -1);
  dfs2(1, -1);
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
