#include <bits/stdc++.h>
using namespace std;
int color[300005], red[300005], blue[300005];
vector<int> arr[300005];
int totalr, totalb;
pair<int, int> cont(int i, int p) {
  int a = 0, b = 0;
  for (int j = 0; j < arr[i].size(); j++) {
    if (arr[i][j] == p) continue;
    pair<int, int> g = cont(arr[i][j], i);
    a += g.first;
    b += g.second;
  }
  red[i] = a;
  blue[i] = b;
  if (color[i] == 1) {
    red[i]++;
    return {a + 1, b};
  } else if (color[i] == 2) {
    blue[i]++;
    return {a, b + 1};
  } else
    return {a, b};
}
void dfs(int i, int p, int &count) {
  for (int j = 0; j < arr[i].size(); j++) {
    if (arr[i][j] == p) continue;
    dfs(arr[i][j], i, count);
  }
  if (p != -1) {
    if (red[i] == 0 && blue[i] == totalb) count++;
    if (red[i] == totalr && blue[i] == 0) count++;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> color[i];
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    cin >> a >> b;
    arr[a].push_back(b);
    arr[b].push_back(a);
  }
  pair<int, int> h = cont(1, -1);
  totalr = h.first;
  totalb = h.second;
  int cnt = 0;
  dfs(1, -1, cnt);
  cout << cnt;
  return 0;
}
