#include <bits/stdc++.h>
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
const double PI = acos(-1), EPS = 1e-7;
const int OO = 0x3f3f3f3f, N = 1e6 + 5, mod = 1e9 + 7;
using namespace std;
long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); }
void file() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int n, red, blue, cnt;
vector<vector<int>> g;
vector<int> col;
pair<int, int> DFS(int v, int p = -1) {
  int r = (col[v] == 1), b = (col[v] == 2);
  for (auto u : g[v])
    if (u != p) {
      auto tmp = DFS(u, v);
      cnt += (tmp.first == red && tmp.second == 0);
      cnt += (tmp.first == 0 && tmp.second == blue);
      r += tmp.first;
      b += tmp.second;
    }
  return make_pair(r, b);
}
int main() {
  file();
  cin >> n;
  g = vector<vector<int>>(n + 1);
  col = vector<int>(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> col[i];
    if (col[i] == 1) red++;
    if (col[i] == 2) blue++;
  }
  n--;
  while (n--) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  DFS(1, -1);
  cout << cnt;
}
