#include <bits/stdc++.h>
using namespace std;
struct Node {
  int c[3];
  Node() { c[0] = c[1] = c[2] = 0; }
};
const int MXSZ = 3e5;
int n;
int a[MXSZ + 5];
vector<int> adj[MXSZ + 5];
Node mem[MXSZ + 5];
void dfs(int u, int prev) {
  mem[u].c[a[u]]++;
  for (int v : adj[u]) {
    if (v == prev) continue;
    dfs(v, u);
    for (int i = 1; i < 3; i++) mem[u].c[i] += mem[v].c[i];
  }
}
int ans = 0, all1, all2;
void check(int u, int prev) {
  int rest1 = 0, rest2 = 0;
  for (int v : adj[u]) {
    if (v == prev) continue;
    rest1 = all1 - mem[v].c[1];
    rest2 = all2 - mem[v].c[2];
    if (min(rest1, rest2) == 0 && min(mem[v].c[1], mem[v].c[2]) == 0) {
      ans++;
    }
    check(v, u);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1, u, v; i < n; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  all1 = mem[1].c[1], all2 = mem[1].c[2];
  check(1, -1);
  cout << ans << '\n';
  return 0;
}
