PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000061. Màu sắc khác nhau (Distinct Colors)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Màu sắc khác nhau (Distinct Colors)

Distinct Colors

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

A tree of nn vertices is rooted at vertex 11. Vertex vv has integer color cvc_v. The subtree of vv consists of vv itself and every descendant of vv when the tree is rooted at 11. For each vertex vv, count how many distinct colors occur in its subtree; repeated occurrences of a color count once.

Input

Line 1 contains nn. Line 2 contains c1,…,cnc_1,\ldots,c_n. The next n−1n-1 lines describe undirected edges u vu\ v.

Output

Print nn integers; the vv-th is the number of distinct colors in the subtree of vv.

Subtasks

General constraints: 1≤n≤2⋅1051\le n\le 2\cdot 10^5; 1≤cv≤1091\le c_v\le 10^9; cạnh tạo thành một cây.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000.

  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000.

Examples

Input

5
2 3 2 2 1
1 2
1 3
3 4
3 5

Output

3 1 2 1 1

Explanation

The subtree of vertex 11 contains colors 1,2,31,2,3; that of vertex 33 contains colors 1,21,2; each leaf has one color.