We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Advanced
  4. Unique Colors

Unique Colors

Problem
Submissions
Leaderboard
Discussions
Editorial

You are given an unrooted tree of nodes numbered from to . Each node has a color, .

Let be the number of different colors in the path between node and node . For each node , calculate the value of , defined as follows:

Your task is to print the value of for each node .

Input Format

The first line contains a single integer, , denoting the number of nodes.
The second line contains space-separated integers, , where each describes the color of node .
Each of the subsequent lines contains space-separated integers, and , defining an undirected edge between nodes and .

Constraints

Output Format

Print lines, where the line contains a single integer denoting .

Sample Input

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

Sample Output

10
9
11
9
12

Explanation

The Sample Input defines the following tree:

Each is calculated as follows:

Author

osmanorhan

Difficulty

Advanced

Max Score

100

Submitted By

2259

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy