#include <bits/stdc++.h>
using namespace std;
class bign {
 public:
  int d[30];
  int deg;
};
int n, m;
int a[300005];
int res = 0, red = 0, blue = 0;
vector<int> g[300005];
pair<int, int> dfs(int v, int p = -1) {
  int x, y;
  if (a[v] == 1)
    x = 1;
  else
    x = 0;
  if (a[v] == 2)
    y = 1;
  else
    y = 0;
  for (auto u : g[v]) {
    if (u != p) {
      pair<int, int> H = dfs(u, v);
      if (H.first == red && H.second == 0) res++;
      if (H.first == 0 && H.second == blue) res++;
      x += H.first;
      y += H.second;
    }
  }
  return pair<int, int>(x, y);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 1) red++;
    if (a[i] == 2) blue++;
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  res = 0;
  dfs(1);
  cout << res;
}
