#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 3e5 + 10;
int a[MAXN], b[MAXN], c[MAXN];
vector<int> e[MAXN];
int ans;
void DFS(int x, int f) {
  if (a[x] == 1) b[x]++;
  if (a[x] == 2) c[x]++;
  for (int y : e[x])
    if (y != f) {
      DFS(y, x);
      b[x] += b[y];
      c[x] += c[y];
      if (b[y] == b[0] && !c[y] || c[y] == c[0] && !b[y]) ans++;
    }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 1) b[0]++;
    if (a[i] == 2) c[0]++;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    if (e[i].size() == 1) {
      DFS(i, 0);
      break;
    }
  cout << ans << endl;
  return 0;
}
