#include <bits/stdc++.h>
using namespace std;
vector<int> Graph[300005];
int Array[300005];
int Ans = 0;
pair<int, int> dfs(int root, int parent, int R, int B) {
  int i, a = 0, b = 0;
  pair<int, int> P;
  for (i = 0; i < int(Graph[root].size()); ++i) {
    if (Graph[root][i] != parent) {
      P = dfs(Graph[root][i], root, R, B);
      if ((P.first == R && P.second == 0) || (P.second == B && P.first == 0))
        ++Ans;
      a += P.first;
      b += P.second;
    }
  }
  if (Array[root] == 1)
    ++a;
  else if (Array[root] == 2)
    ++b;
  return {a, b};
}
int main() {
  int N, i, u, v, R = 0, B = 0;
  scanf("%d", &N);
  for (i = 1; i <= N; ++i) {
    scanf("%d", Array + i);
    if (Array[i] == 1)
      ++R;
    else if (Array[i] == 2)
      ++B;
  }
  for (i = 1; i < N; ++i) {
    scanf("%d%d", &u, &v);
    Graph[u].emplace_back(v);
    Graph[v].emplace_back(u);
  }
  if (R == 0 || B == 0)
    printf("%d\n", N - 1);
  else {
    dfs(1, 0, R, B);
    printf("%d\n", Ans);
  }
  return 0;
}
