#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
vector<vector<int>> adj(N);
int color[N];
int nice = 0;
int totalRed = 0, totalBlue = 0;
bool vis[N];
pair<int, int> dfs(int node) {
  vis[node] = 1;
  int redSoFar = color[node] == 2, blueSoFar = color[node] == 1;
  vector<pair<int, int>> colors;
  for (auto i : adj[node]) {
    if (vis[i]) continue;
    auto curColors = dfs(i);
    colors.push_back(curColors);
    redSoFar += curColors.first;
    blueSoFar += curColors.second;
  }
  for (auto i : colors) {
    if ((i.second == 0 && i.first == totalRed) ||
        (i.second == totalBlue && i.first == 0))
      nice++;
  }
  return {redSoFar, blueSoFar};
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &color[i]);
    totalBlue += color[i] == 1;
    totalRed += color[i] == 2;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  if (totalBlue == 0 || totalRed == 0) return !printf("%d", n - 1);
  dfs(1);
  printf("%d", nice);
  return 0;
}
