#include <bits/stdc++.h>
using namespace std;
const int N = int(3e5) + 5;
const int INF = int(1e9) + 7;
const long long MINF = 1e18;
int n;
int a[N];
vector<int> g[N];
struct st {
  int xx, yy;
} dp[N];
bool used[N];
void dfs(int v) {
  used[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    if (used[g[v][i]] == 0) {
      dfs(g[v][i]);
      dp[v].xx += dp[g[v][i]].xx;
      dp[v].yy += dp[g[v][i]].yy;
    }
  }
  if (a[v] == 2) {
    dp[v].yy++;
  }
  if (a[v] == 1) {
    dp[v].xx++;
  }
  return;
}
int ans;
void dfs_ans(int v) {
  used[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    if (used[g[v][i]] == 0) {
      int to = g[v][i];
      if (dp[to].xx == 0 && dp[to].yy > 0) {
        if (dp[1].yy - dp[to].yy == 0) {
          ans++;
        }
      } else if (dp[to].xx > 0 && dp[to].yy == 0) {
        if (dp[1].xx - dp[to].xx == 0) {
          ans++;
        }
      }
      dfs_ans(g[v][i]);
    }
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    int l, r;
    cin >> l >> r;
    g[l].push_back(r);
    g[r].push_back(l);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) used[i] = 0;
  dfs_ans(1);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  int TT = 1;
  while (TT) {
    solve();
    TT--;
  }
  return 0;
}
