#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, dp[N], a[N], ans, k, dp2[N], t;
vector<int> g[N];
void dfs(int x, int p) {
  if (a[x] == 1) dp[x]++;
  if (a[x] == 2) dp2[x]++;
  for (int i = 0; i < g[x].size(); ++i) {
    int y = g[x][i];
    if (y == p) continue;
    dfs(y, x);
    dp[x] += dp[y];
    dp2[x] += dp2[y];
    if (dp[y] == 0 && dp2[y] == t) ans++;
    if (dp[y] == k && dp2[y] == 0) ans++;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    k += (a[i] == 1);
    t += (a[i] == 2);
  }
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, 0);
  cout << ans;
}
