#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
long long int t_blue, t_red;
long long int n, a[N], red[N], blue[N], black[N], sz[N];
vector<pair<int, int>> g[N];
void dfs(int n, int edg) {
  if (a[n] == 2) {
    blue[edg] = 1;
  }
  if (a[n] == 1) {
    red[edg] = 1;
  }
  if (a[n] == 0) {
    black[edg] = 1;
  }
  sz[edg] = 1;
  for (auto x : g[n]) {
    if (x.second == edg) continue;
    dfs(x.first, x.second);
    blue[edg] += blue[x.second];
    red[edg] += red[x.second];
    black[edg] += black[x.second];
    sz[edg] += sz[x.second];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 2) t_blue++;
    if (a[i] == 1) t_red++;
  }
  for (int x, y, i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    g[x].push_back({y, i});
    g[y].push_back({x, i});
  }
  dfs(1, 0);
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    if ((red[i] > 0 && blue[i] == 0 && t_blue - blue[i] > 0 &&
         t_red - red[i] == 0) ||
        (red[i] == 0 && blue[i] > 0 && t_blue - blue[i] == 0 &&
         t_red - red[i] > 0)) {
      ans++;
    }
  }
  cout << ans << '\n';
}
