#SRD0000097. Những màu xuất hiện nhiều nhất (Lomsat gelral)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những màu xuất hiện nhiều nhất (Lomsat gelral)
Lomsat gelral
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a tree with vertices numbered from to , rooted at vertex . Vertex has positive integer color .
The subtree of contains vertex and all its descendants. Let be the number of vertices of color in this subtree.
A color is called dominating in the subtree if its frequency is maximal among all colors present there. Several colors may be tied for the maximum.
For every vertex , output the sum of the numeric color IDs of all dominating colors in its subtree. Count each dominating color ID exactly once, regardless of how many vertices have that color.
Input
The first line contains . The second line contains color IDs . Each of the next lines contains two vertices representing an edge.
Output
Print integers; the -th is the sum of dominating color IDs in the subtree rooted at .
Subtasks
- Subtask 1 (10 points): .
- Subtask 2 (20 points): .
- Subtask 3 (30 points): .
- Subtask 4 (40 points): .
Conditions shared by all subtasks: For every group: , , . The edges form an undirected tree rooted at .
Examples
Example 1
Input
4
1 2 3 4
1 2
2 3
2 4
Output
10 9 3 4
Explanation
For vertex , colors each occur once, so all four dominate and the sum is . Vertex has subtree colors , summing to . Each leaf's subtree has just its own color, giving and .
Example 2
Input
15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
Output
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
Explanation
In example 2, colors each occur five times in the entire tree, so their sum is for the root. At vertex , colors and tie for the highest frequency, producing .