#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100000;
struct edge
{
int to, nxt;
} e[N<<1];
struct node
{
int lc, rc; // 左、右子结点的位置
int com; // 这棵树中共有颜色的数量
int num; // 对叶子有用,表示这棵树中这种颜色的结点数
} tree[18*N]; // 存树结点的空间
int sz; // 树结点总数
int c[N+1]; // 结点i的颜色
int sum[N+1]; // 颜色i的总数
int head[N+1]; // 前向星链头
int root[N+1]; // 为第i个结点建的线段树的根所在位置
int ans[N]; // 答案数组
void add_edge(int f, int t, int id)
{
e[id].to = t;
e[id].nxt = head[f];
head[f] = id;
}
inline void pushup(int x)
{
/* 不是叶子的结点,只需记录共有颜色的数量 */
tree[x].com = tree[tree[x].lc].com + tree[tree[x].rc].com;
}
int build(int c, int l, int r)
{
int rt = ++sz;
tree[rt].lc = tree[rt].rc = tree[rt].num = 0;
if(l == r)
{
tree[rt].num = 1;
tree[rt].com = tree[rt].num < sum[c];
}
else
{
int m = l + r >> 1;
/* 不需要建整棵树,只要建一条链 */
if(c > m)
tree[rt].rc = build(c, m+1, r);
else
tree[rt].lc = build(c, l, m);
pushup(rt);
}
return rt;
}
void merge(int &fa, int ch, int l, int r)
{
if(!fa || !ch)
{
/* 若父结点那棵线段树的这个结点为空,
* 就直接重用子结点的线段树的这条链
*/
if(!fa)
fa = ch;
return;
}
if(l != r)
{
int m = l + r >> 1;
merge(tree[fa].lc, tree[ch].lc, l, m);
merge(tree[fa].rc, tree[ch].rc, m+1, r);
pushup(fa);
}
else
{
/* 将子树里这种颜色的结点数加到父结点的树 */
tree[fa].num += tree[ch].num;
/* 这种颜色的结点不全在这棵树中,
* 则这种颜色是共有颜色
*/
tree[fa].com = tree[fa].num < sum[l];
}
}
void dfs(int rt, int fa, int eid, int n)
{
/* 先给根结点“建棵树” */
root[rt] = build(c[rt], 1, n);
for(int i=head[rt]; ~i; i=e[i].nxt)
{
if(e[i].to == fa)
continue;
/* 先递归处理子结点 */
dfs(e[i].to, rt, i, n);
/* 然后将子结点信息合并上来 */
merge(root[rt], root[e[i].to], 1, n);
}
/* 加边时同一条边加了两次,
* 这个映射找出此边在输入时的序号
*/
if(rt != 1)
ans[eid/2+1] = tree[root[rt]].com;
}
int main()
{
tree[0].lc = tree[0].rc = tree[0].com = tree[0].num = 0;
int n;
while(~scanf("%d", &n))
{
memset(sum, 0, sizeof sum);
for(int i=1; i<=n; ++i)
{
scanf("%d", c+i);
++sum[c[i]];
}
memset(head, -1, sizeof head);
for(int i=1, a, b, id=0; i<n; ++i)
{
scanf("%d%d", &a, &b);
add_edge(a, b, id++);
add_edge(b, a, id++);
}
sz = 0;
memset(root, 0, sizeof root);
dfs(1, 0, 0, n);
for(int i=1; i<n; ++i)
printf("%d\n", ans[i]);
}
return 0;
}