#include <bits/stdc++.h>
using namespace std;
int a[300001];
int n, red, blue;
int total;
pair<int, int> dfs(vector<vector<int> > &graph, int v, vector<int> &visited) {
  pair<int, int> p = {0, 0};
  if (visited[v] == 0) {
    visited[v] = 1;
    for (int i = 0; i < graph[v].size(); ++i) {
      if (visited[graph[v][i]] != 1) {
        pair<int, int> q = dfs(graph, graph[v][i], visited);
        if (q.first == red && q.second == 0)
          total++;
        else if (q.first == 0 && q.second == blue)
          total++;
        p.first += q.first;
        p.second += q.second;
      }
    }
  }
  if (a[v] == 1)
    p.first += 1;
  else if (a[v] == 2)
    p.second += 1;
  return p;
}
int main() {
  cin >> n;
  red = 0;
  blue = 0;
  total = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    if (a[i] == 1) red++;
    if (a[i] == 2) blue++;
  }
  vector<vector<int> > graph(n);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    graph[x - 1].push_back(y - 1);
    graph[y - 1].push_back(x - 1);
  }
  vector<int> visited(n, 0);
  dfs(graph, 0, visited);
  cout << total << endl;
  return 0;
}
