#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
long long M = 1e9 + 7;
const double PI = 3.141592653589793238460;
long long MOD = 998244353;
bitset<N> bs;
int color[300010];
vector<int> adj[300010];
int red, blue;
long long ans;
pair<int, int> dfs(int s, int p) {
  int r = (color[s] == 1), b = (color[s] == 2);
  for (int x : adj[s]) {
    if (x != p) {
      auto temp = dfs(x, s);
      if (temp.first == red && temp.second == 0) ans++;
      if (temp.first == 0 && temp.second == blue) ans++;
      r += temp.first;
      b += temp.second;
    }
  }
  return {r, b};
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int n, u, v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> color[i];
    if (color[i] == 1) {
      red++;
    } else if (color[i] == 2) {
      blue++;
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    cin >> u >> v;
    adj[u].push_back(v), adj[v].push_back(u);
  }
  dfs(1, 0);
  cout << ans;
}
