Dominating color - MarisaOJ: Marisa Online Judge

Dominating color

Time limit: 2000 ms
Memory limit: 256 MB
Given a tree of n vertices, vertex i has color Ai. Given q queries of the form (u,v), find the dominating color on the simple path from u to v. If the path from u to v containing k edges, color c is called dominating if it appears at least ⌈k2⌉+1 times. ### Input - The first line contains two integers n,q. - The second line contains n integers Ai. - The next n−1 lines, each line contains two integers u,v, there is an edge between u and v. - The next q lines, each line contains two integers u,v, a query. ### Output - Print the answer for each query, or print -1 if no answer exists. ### Constraints - 1≤n≤105. - 1≤Ai≤109. - 1≤u,v≤n. ### Example Input: 431222122324122314 Output: -122