#include <bits/stdc++.h>
using namespace std;
const int N = 300 * 1000 + 13;
int n;
int ar[N];
vector<int> g[N];
int red, blue;
int ans;
pair<int, int> dfs(int v, int p = -1) {
  int r = (ar[v] == 1);
  int b = (ar[v] == 2);
  for (auto u : g[v]) {
    if (u != p) {
      auto tmp = dfs(u, v);
      ans += (tmp.first == red && tmp.second == 0);
      ans += (tmp.first == 0 && tmp.second == blue);
      r += tmp.first;
      b += tmp.second;
    }
  }
  return make_pair(r, b);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ar[i] = x;
    red += (x == 1);
    blue += (x == 2);
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x;
    cin >> y;
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  ans = 0;
  dfs(0);
  cout << ans;
  return 0;
}
