Fruit Tree

각 정점에 과일 종류가 적힌 N개 정점 트리에서 Q개의 경로 질의마다 경로 위에서 절반을 초과해 등장하는 과일 종류를 출력하고, 없으면 -1을 출력한다.

어려움9트리세그먼트 트리DFS이분 탐색아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

In the backyard of Seoul Science High School, there is a magical tree with NN vertices, where every vertex contains a single fruit. (A tree is a connected undirected graph with N1N-1 edges.) 

Although it is prohibited to pick any fruits from the tree, students naturally want to secretly pick some fruit to eat. To prevent being caught by the teacher, they use the following procedure to choose a fruit to pick:

  • Choose two vertex s,es, e in the tree, and consider all fruits lying in the unique path from ss to ee. Fruits in vertex s,es, e are also considered.
  • Among the fruits in a path, if there is a type of fruit that forms a \textit{majority}, student pick that fruit and eat. A type of fruit forms a majority if the count of such fruits in a path is strictly larger than half of the total number of fruits in the path.

Of course, they are very nice students, so they never actually pick any of the fruits. They simply think of it. :)

Being exceptionally nice students, they naturally extended their thought experiment as a query problem. Thus, given QQ independent queries, you should find the answer or state that no majority exists. Can you solve it?

입력

The first line contains two integers N,QN, Q. (1N,Q2500001 \le N, Q \le 250 000).

In the next line, NN integers c_ic\_i is given, denoting the type of fruit in vertex ii. (1c_iN1 \le c\_i \le N).

In the next N1N-1 lines, two integers a_i,b_ia\_i, b\_i denoting endpoints of each edge are given. (1a_i,b_iN,a_ib_i1 \le a\_i, b\_i \le N, a\_i \neq b\_i).

In the next QQ lines, two integers s_i,e_is\_i, e\_i denoting two endpoints of each path are given. (1s_i,e_iN1 \le s\_i, e\_i \le N).

출력

Print QQ lines. For each line, print a single integer denoting the type of fruit that forms a majority in a given path. If there exists no majority in the given path, print 1-1.\