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 $A_i$. 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$ vertices then color $c$ is called dominating if it appears at least $\lceil \frac{k}{2} \rceil + 1$ times. ### Input - The first line contains two integers $n, q$. - The second line contains $n$ integers $A_i$. - The next $n$ 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 \le n \le 10^5$. - $1 \le A_i \le 10^9$. - $1 \le u, v \le n$. ### Example Input: ``` 4 3 1 2 2 2 1 2 2 3 2 4 1 2 2 3 1 4 ``` Output: ``` -1 2 2 ```