#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int n, m;
int color[maxn];
int red, blue;
int ans;
vector<int> G[maxn];
pair<int, int> dfs(int pre, int now) {
  int bl = 0, re = 0;
  if (color[now] == 1) ++re;
  if (color[now] == 2) ++bl;
  for (int i = 0; i < G[now].size(); ++i) {
    int to = G[now][i];
    if (to == pre) continue;
    pair<int, int> t = dfs(now, to);
    ans += (t.first == red && t.second == 0);
    ans += (t.first == 0 && t.second == blue);
    bl += t.second;
    re += t.first;
  }
  return make_pair(re, bl);
}
int main() {
  cin >> n;
  m = n - 1;
  for (int i = 1; i <= n; ++i) {
    cin >> color[i];
    if (color[i] == 1) ++red;
    if (color[i] == 2) ++blue;
  }
  for (int i = 1; i <= m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(0, 1);
  cout << ans << endl;
}
