#include <bits/stdc++.h>
using namespace std;
vector<int> ve[300005];
int n;
int a[300005];
int red = 0, blue = 0;
struct sair {
  int red, blue;
} ans[300005];
void dfs(int pos, int pre) {
  if (a[pos] == 1)
    ans[pos].red++;
  else if (a[pos] == 2)
    ans[pos].blue++;
  for (int i = 0; i < ve[pos].size(); i++) {
    if (ve[pos][i] != pre) {
      dfs(ve[pos][i], pos);
      ans[pos].blue += ans[ve[pos][i]].blue;
      ans[pos].red += ans[ve[pos][i]].red;
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans[i].blue = ans[i].red = 0;
    if (a[i] == 1)
      red++;
    else if (a[i] == 2)
      blue++;
  }
  int u, v;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    ve[u].push_back(v);
    ve[v].push_back(u);
  }
  dfs(1, 0);
  int aaa = 0;
  for (int i = 2; i <= n; i++) {
    if ((ans[i].red == red && ans[i].blue == 0) ||
        (ans[i].red == 0 && ans[i].blue == blue)) {
      aaa++;
    }
  }
  cout << aaa << endl;
}
