#include <bits/stdc++.h>
using namespace std;
vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int MAX = 1e5 + 55;
const int inf = 1e9 + 77;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-7;
struct node {
  int blue, red;
  node(int _blue = 0, int _red = 0) {
    blue = _blue;
    red = _red;
  }
  node(const node& temp) {
    blue = temp.blue;
    red = temp.red;
  }
};
node pre[3 * MAX];
int color[3 * MAX];
vector<int> adj[3 * MAX];
int vis[3 * MAX];
vector<pair<int, int> > edges;
int dist[3 * MAX];
void dfs(int u, int depth) {
  vis[u] = 1;
  dist[u] = depth;
  if (color[u] == 1)
    pre[u].red = 1;
  else if (color[u] == 2)
    pre[u].blue = 1;
  for (auto v : adj[u]) {
    if (vis[v]) continue;
    dfs(v, depth + 1);
    pre[u].blue += pre[v].blue;
    pre[u].red += pre[v].red;
  }
}
int tot[3];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &color[i]);
    tot[color[i]]++;
  }
  int m = n - 1;
  while (m--) {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    --x;
    --y;
    adj[x].push_back(y);
    adj[y].push_back(x);
    edges.push_back(make_pair(x, y));
  }
  dfs(0, 0);
  int res = 0;
  for (auto e : edges) {
    int u = e.first;
    int v = e.second;
    bool test = false;
    if (dist[v] > dist[u]) swap(u, v);
    if ((pre[u].blue == tot[2] && pre[u].red == 0) ||
        (pre[u].blue == 0 && pre[u].red == tot[1]))
      test = true;
    if (test) ++res;
  }
  printf("%d", res);
  return 0;
}
