#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>


#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;

ll modd = 1000 * 1000 * 1000 + 7;

template<class T>
class depth_first_search_iterator {
  public:
    stack<pair<T,T>> todo;
    function<bool(T)> notvisited;
    function<void(T)> mark_visited;
    function<set<T>(T)> neighbors;

    depth_first_search_iterator() {}

    depth_first_search_iterator(T start, function<bool(T)> notv, function<void(T)> mark_v, function<set<T>(T)> neigh) :
      notvisited(notv), mark_visited(mark_v), neighbors(neigh), end_(false), current(start), prev(start) {
          todo.push(make_pair(start, start)); operator++();
    }

    void operator++() {
        end_ = true;
        while (!todo.empty()) {
            current = todo.top().first; prev = todo.top().second; todo.pop();
            if (notvisited(current)) {
                mark_visited(current);
                for (const T x : neighbors(current)) {  todo.push(make_pair(x, current));   }
                end_ = false;
                break;
            }
        }
    }

    T operator*() {     return current;    }

    T previous() {  return prev;  }

    bool end() {  return end_;  }

  private:
    T current; // contains current node
    T prev;    // contains previous node
    bool end_;
};

template<class T>
class breadth_first_search_iterator {
  public:
    queue<pair<T,T>> todo;
    function<bool(T)> notvisited;
    function<void(T)> mark_visited;
    function<set<T>(T)> neighbors;

    breadth_first_search_iterator() {}

    breadth_first_search_iterator(T start, function<bool(T)> notv, function<void(T)> mark_v, function<set<T>(T)> neigh) :
      notvisited(notv), mark_visited(mark_v), neighbors(neigh), end_(false), current(start), prev(start) {
          todo.push(make_pair(start, start)); operator++();
    }

    void operator++() {
        end_ = true;
        while (!todo.empty()) {
            current = todo.front().first; prev = todo.front().second; todo.pop();
            if (notvisited(current)) {
                mark_visited(current);
                for (const T x : neighbors(current)) {  todo.push(make_pair(x, current));   }
                end_ = false;
                break;
            }
        }
    }

    T operator*() {     return current;    }

    T previous() {  return prev;  }

    bool end() {  return end_;  }

  private:
    T current;   // current node
    T prev;      // previous node
    bool end_;
};

template<class T>
class Graph {
    // allows only single connection between two vertices
    public:
      vector<T> vertices;
      vector<set<int>> neighbors;
      depth_first_search_iterator<int> dfs_iterator;
      breadth_first_search_iterator<int> bfs_iterator;
      vector<bool> visited;

      Graph() {}

      Graph(int n, T default_val = 0) {  for (int i = 0; i < n; ++i) {  AddVertex(default_val);  }   }

      void AddVertex(T val) {
          vertices.push_back(val);
          neighbors.push_back(set<int>());      }

      bool EdgeExists(int i, int j) {
          return (neighbors[i].find(j) != neighbors[i].end());
     }

      void AddEdge(int i, int j) {
          if (!EdgeExists(i,j)) {
            neighbors[i].insert(j);
            if (i != j) {         neighbors[j].insert(i);         } 
          }
      }

      T& operator[](int i) {      return vertices[i];      }

      void RemoveEdge(int i, int j) {
          if (EdgeExists(i,j)) {
            neighbors[i].erase(find(neighbors[i].begin(), neighbors[i].end(), j));
            neighbors[j].erase(find(neighbors[j].begin(), neighbors[j].end(), i));
          }
      }

      int vertices_count() {   return vertices.size();  }

      void dfs_init(int start) {
          visited = vector<bool>(vertices.size(), false);
          dfs_iterator = depth_first_search_iterator<int>(start, [this](int x){ return !visited[x]; }, [this](int x) { visited[x] = true;  },
            [this](int x) {  return neighbors[x];  } );
      }

      void bfs_init(int start) {
          visited = vector<bool>(vertices.size(), false);
          bfs_iterator = breadth_first_search_iterator<int>(start, [this](int x){ return !visited[x]; }, [this](int x) { visited[x] = true;  },
            [this](int x) {  return neighbors[x];  } );
      }

      void print_() {
          forr(i, 0, vertices.size()) {
              for (auto x : neighbors[i]) {
                  if (i <= x) {  cout << i << "-" << x << endl;  }                  
              }
          }
      }
};


template<class T>
class DirectedGraph {
    // allows only single connection between two vertices
    public:
      vector<T> vertices;
      vector<set<int>> neighbors;
      depth_first_search_iterator<int> dfs_iterator;
      breadth_first_search_iterator<int> bfs_iterator;
      vector<bool> visited;

      DirectedGraph() {}

      DirectedGraph(int n, T default_val = 0) {  for (int i = 0; i < n; ++i) {  AddVertex(default_val);  }   }

      bool EdgeExists(int i_from, int j_to) {
          return (neighbors[i_from].find(j_to) != neighbors[i_from].end());
      }

      void AddVertex(T val) {
          vertices.push_back(val);
          neighbors.push_back(set<int>());      }

      void AddEdge(int i_from, int j_to) {
          if (!EdgeExists(i_from, j_to)) {     neighbors[i_from].insert(j_to);      }
      }

      T& operator[](int i) {      return vertices[i];      }

      void RemoveEdge(int i_from, int j_to) {
          if (EdgeExists(i_from, j_to)) {
          neighbors[i_from].erase(find(neighbors[i_from].begin(), neighbors[i_from].end(), j_to));      } }

      DirectedGraph<T> Transpose() {
          DirectedGraph<T> ret;
          for (auto x : vertices) {  ret.AddVertex(x);   }
          for (int i_from = 0; i_from < vertices.size(); ++i_from) {
              for (int j_to : neighbors[i_from]) {
                  ret.AddEdge(j_to, i_from);
              }
          }
          return ret;      }

      int vertices_count() {   return vertices.size();  }

      void dfs_init(int start) {
          visited = vector<bool>(vertices.size(), false);
          dfs_iterator = depth_first_search_iterator<int>(start, [this](int x){ return !visited[x]; }, [this](int x) { visited[x] = true;  },
            [this](int x) {  return neighbors[x];  } );
      }

      void bfs_init(int start) {
          visited = vector<bool>(vertices.size(), false);
          bfs_iterator = breadth_first_search_iterator<int>(start, [this](int x){ return !visited[x]; }, [this](int x) { visited[x] = true;  },
            [this](int x) {  return neighbors[x];  } );
      }

      void print_() {
          forr(i, 0, vertices.size()) {
              for (auto x : neighbors[i]) {
                  cout << i << "->" << x << endl;
              }
          }
      }

};

template<class T>
class DFST {
    public:
      vector<pair<int,int>> DFSTour;
      vector<int> to_v;  // contains index in DFSTour where the down edge to the vertex is contained
      vector<int> from_v;    // contains index in DFSTour where the up edge frrom the vertex is contained

      DFST(DirectedGraph<T>& g, int root) : from_v(vector<int>(g.vertices_count(), 0)), to_v(vector<int>(g.vertices_count(), 0)) {
          DFSTour.push_back(make_pair(-1, root));   // fake start in edge of the tour
          to_v[root] = 0;
          DFStourhelper(g, root);
          DFSTour.push_back(make_pair(root, -1));
          from_v[root] = DFSTour.size()-1;    // fake end out edge of the tour
      }

      void DFStourhelper(DirectedGraph<T>& g, int root) {
          for (auto n : g.neighbors[root]) {
              DFSTour.push_back(make_pair(root, n));
              to_v[n] = DFSTour.size()-1;
              DFStourhelper(g, n);
              DFSTour.push_back(make_pair(n, root));
              from_v[n] = DFSTour.size()-1;
          }
      }

      DFST(Graph<T>& g, int root, int parent) : from_v(vector<int>(g.vertices_count(), 0)), to_v(vector<int>(g.vertices_count(), 0)) {
          DFSTour.push_back(make_pair(-1, root));   // fake start in edge of the tour
          to_v[root] = 0;
          DFStourhelper(g, root, parent);
          DFSTour.push_back(make_pair(root, -1));
          from_v[root] = DFSTour.size()-1;    // fake end out edge of the tour
      }

      void DFStourhelper(Graph<T>& g, int root, int parent) {
          for (auto n : g.neighbors[root]) {
            if (n!=parent) {
              DFSTour.push_back(make_pair(root, n));
              to_v[n] = DFSTour.size()-1;
              DFStourhelper(g, n, root);
              DFSTour.push_back(make_pair(n, root));
              from_v[n] = DFSTour.size()-1;
            }
          }
      }

};

template<class T>
class SegmentTree2 {
    public:
     
      SegmentTree2(const vector<T>& data, function<T(T,T)> f_, T zero_value = 0) : zv(zero_value), f(f_) {   
          Initialize(data);
      }

      SegmentTree2(int n, function<T(T,T)> f_, T zero_value = 0) : zv(zero_value), f(f_) {
          vector<T> temp(n, zv);
          Initialize(temp);
      }

      T operator[](int i) {
          return tree[B+i];
      }

      T GetEvaluation(int L, int R) {   // "min/max" on interval [L,R); 0-based indexing, as usual
        if (R<=L) {  return zv;  }
        return GetEvaluationHelper(L, R, 0, B, 1);
      }


      void SetVal(int i, T val) {
			tree[B+i] = val;
			for(int j = (B + i) / 2; j >= 1; j /= 2) {
				tree[j] = f(tree[2*j], tree[2*j+1]);
			}
      }

      private:
        vector<T> tree;
        int B;  // power of two greater than size of input data
        T zv;
        function<T(T,T)> f;

        void Initialize(const vector<T>& data) {
    	  B = 1;
          while(B < data.size()) {B *= 2;  }
          tree = std::move(vector<T>(2*B, zv));
          copy(data.begin(), data.end(), next(tree.begin(), B));
          for(int i = B - 1; i >= 1; --i) {
  	  	     tree[i] = f(tree[2*i], tree[2*i+1]);
          }
        }

      T GetEvaluationHelper(int L, int R, int start, int length, int tree_index) {
          if (L==R) {  return zv; }
          if ((L==start) && (R==start+length)) {   return tree[tree_index];  }
          int midpoint = start + length/2;

          T left_ = zv, right_ = zv;
          if (L<=min(midpoint,R)) {
            left_ = GetEvaluationHelper(L, min(midpoint,R), start, length/2, 2*tree_index);
          }
          if (max(midpoint,L)<=R) {
            right_ = GetEvaluationHelper(max(midpoint,L), R, midpoint, length/2, 2*tree_index+1);
          }
          return f(left_, right_);
      }

};



int main()   {

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n);
        Graph<int> g_(n);
        DirectedGraph<int> g(n);
        forr(i,0,n) {
            readint(cc); g[i] = cc;
        }
        forr(i,0,n-1) {
            readint(uu); readint(vv);
            g_.AddEdge(uu-1, vv-1);
        }
        g_.dfs_init(0); ++g_.dfs_iterator;
        while (!g_.dfs_iterator.end()) {
            g.AddEdge(g_.dfs_iterator.previous(), *g_.dfs_iterator);
            ++g_.dfs_iterator;
        }

        DFST<int> dfst(g, 0);
        vector<int> depths(n);
        g.bfs_init(0); ++g.bfs_iterator;
        while (!g.bfs_iterator.end()) {
            depths[*g.bfs_iterator] = 1 + depths[g.bfs_iterator.previous()];
            ++g.bfs_iterator;
        }
        SegmentTree2<ll> dfst_seg(dfst.DFSTour.size(), [](ll x, ll y){ return x+y; }, 0);
        forr(i,0,g.vertices_count()) {
            dfst_seg.SetVal(dfst.to_v[i], 1);
        }

        vector<vector<pair<int,int>>> vertices(n+1);
        forr(i,0,n) {
            vertices[g[i]].push_back(make_pair(depths[i],i));
        }
        forr(c,1,n) {
            ll answer = 0;
            sort(vertices[c].begin(), vertices[c].end());
            reverse(vertices[c].begin(), vertices[c].end());
            stack<int> cleanup;
            for(auto x : vertices[c]) {
                ll total = dfst_seg.GetEvaluation(dfst.to_v[0], dfst.from_v[0]);
                ll tot_sum = 0, tot_sum2 = 0;
                for(auto v : g.neighbors[x.second]) {
                    ll temp = dfst_seg.GetEvaluation(dfst.to_v[v], dfst.from_v[v]);
                    tot_sum += temp;
                    tot_sum2 += temp*temp;
                }                
                dfst_seg.SetVal(dfst.to_v[x.second], 1 - (tot_sum+1));
                ll temp2 = total - tot_sum - 1;
                tot_sum += temp2; tot_sum2 += temp2*temp2;
                answer += total + (tot_sum*tot_sum - tot_sum2)/2;
                cleanup.push(dfst.to_v[x.second]);
            }
            while (!cleanup.empty()) {
                dfst_seg.SetVal(cleanup.top(), 1);
                cleanup.pop();
            }
            cout << answer << endl;
        }

    }
    return 0;
}


Measured memory_bytes: 94424