#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int color[maxn];
vector<int> G[maxn];
struct st {
  int blue;
  int red;
};
st point[maxn];
int vis[maxn];
int dft[maxn];
st dfs(int id, int step) {
  vis[id] = 1;
  dft[id] = step;
  int siz = G[id].size();
  int i;
  st temp;
  temp.blue = temp.red = 0;
  for (i = 0; i < siz; ++i) {
    st ge;
    int v;
    v = G[id][i];
    if (!vis[v]) {
      ge = dfs(v, step + 1);
      temp.blue += ge.blue;
      temp.red += ge.red;
    }
  }
  if (color[id] == 1) {
    temp.red++;
  } else if (color[id] == 2) {
    temp.blue++;
  }
  point[id] = temp;
  return point[id];
}
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int i;
  int sumcolor[3] = {0};
  for (i = 1; i < n + 1; ++i) {
    cin >> color[i];
    sumcolor[color[i]]++;
  }
  for (i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  int ans = 0;
  int j;
  for (i = 1; i < n + 1; ++i) {
    int len = G[i].size();
    for (j = 0; j < len; ++j) {
      int v = G[i][j];
      int k = dft[i] < dft[v] ? v : i;
      if ((point[k].blue == sumcolor[2] && point[k].red == 0) ||
          (point[k].red == sumcolor[1] && point[k].blue == 0)) {
        ans++;
      }
    }
  }
  cout << ans / 2 << endl;
  return 0;
}
