Colorful path - MarisaOJ: Marisa Online Judge

Colorful path

Time limit: 1000 ms
Memory limit: 256 MB
For a tree with n vertices, color vertex i with color Ai. Define f(x,y) as the number of distinct colors on the simple path from x to y. For each vertex i from 1 to n, calculate ∑j=1nf(i,j). ### Input - The first line contains an integer n. - The second line contains n integers Ai. - The next n−1 lines each contain two integers u,v indicating an edge between vertices u and v. ### Output - Output n integers, where the i-th integer is ∑j=1nf(i,j). ### Constraints - 1≤n,Ai≤105. ### Example Input: 51232312232415 Output: 10911912