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

#LCA0000083. Ngôi nhà mới của sóc (Squirrel’s New Home)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ngôi nhà mới của sóc (Squirrel’s New Home)

Squirrel’s New Home

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

A squirrel lives in a house with nn rooms connected by n−1n-1 corridors forming a tree. A visitor follows the itinerary a1,a2,…,ana_1,a_2,\ldots,a_n, using the unique route between consecutive rooms. The visitor takes one candy upon first arriving at a1a_1 and whenever entering another room along the walk. No extra candy is taken at a junction between consecutive itinerary segments. At the final destination ana_n, no candy is taken on the final arrival because dinner is served there. Find the minimum candies to put in each room.

Input

First line: nn. Second line: nn room labels a1,…,ana_1,\ldots,a_n. Next n−1n-1 lines: edges u,vu,v.

Output

Print nn lines; line vv contains the candies required in room vv.

Subtasks

  • Subtask 1 (20 points): 2≤n≤352\le n\le 35; 1≤ai,u,v≤n1\le a_i,u,v\le n; the itinerary has nn positions and the tree has n−1n-1 edges, edges form a tree.

  • Subtask 2 (30 points): 2≤n≤12002\le n\le 1200; 1≤ai,u,v≤n1\le a_i,u,v\le n; the itinerary has nn positions and the tree has n−1n-1 edges, edges form a tree.

  • Subtask 3 (50 points): 2≤n≤3000002\le n\le 300000; 1≤ai,u,v≤n1\le a_i,u,v\le n; the itinerary has nn positions and the tree has n−1n-1 edges, edges form a tree.

Examples

Input

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

Output

1
2
1
2
1

Explanation

The actual walk is 1→2→4→5→4→2→3→21\to2\to4\to5\to4\to2\to3\to2. Rooms 22 and 44 each require two candies; none is taken on the final arrival at room 22.