#include <bits/stdc++.h>
using namespace std;
struct Graph {
  vector<int> path;
  int b, c;
  int k;
  bool vis;
  Graph(void) : b(0), c(0), vis(false) {}
};
void dfs(vector<Graph>& g, int v) {
  g[v].vis = true;
  for (int u : g[v].path)
    if (!g[u].vis) {
      dfs(g, u);
      g[v].b += g[u].b;
      g[v].c += g[u].c;
    }
  g[v].vis = false;
}
int move(vector<Graph>& g, int v, int u) {
  int x = g[v].b, y = g[v].c;
  g[v].b -= g[u].b;
  g[v].c -= g[u].c;
  int res = 0;
  if (g[u].b == 0 && g[v].c == 0) res++;
  g[u].b = x, g[u].c = y;
  return res;
}
int result(vector<Graph>& g, int v) {
  g[v].vis = true;
  int res = 0;
  for (int u : g[v].path)
    if (!g[u].vis) {
      res += move(g, v, u);
      res += result(g, u);
      res += move(g, u, v);
    }
  return res;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<Graph> g(n);
  for (auto& i : g) {
    cin >> i.k;
    if (i.k != 0) (i.k == 1 ? i.b : i.c)++;
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b, a--, b--;
    g[a].path.push_back(b);
    g[b].path.push_back(a);
  }
  dfs(g, 0);
  cout << result(g, 0) << "\n";
  return 0;
}
