각 정점에 과일 종류가 적힌 N개 정점 트리에서 Q개의 경로 질의마다 경로 위에서 절반을 초과해 등장하는 과일 종류를 출력하고, 없으면 -1을 출력한다.
어려움9트리세그먼트 트리DFS이분 탐색아직 제출이 없습니다시간 제한3초메모리 제한1024 MBIn the backyard of Seoul Science High School, there is a magical tree with N vertices, where every vertex contains a single fruit. (A tree is a connected undirected graph with N−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:
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 Q independent queries, you should find the answer or state that no majority exists. Can you solve it?
The first line contains two integers N,Q. (1≤N,Q≤250000).
In the next line, N integers c_i is given, denoting the type of fruit in vertex i. (1≤c_i≤N).
In the next N−1 lines, two integers a_i,b_i denoting endpoints of each edge are given. (1≤a_i,b_i≤N,a_i=b_i).
In the next Q lines, two integers s_i,e_i denoting two endpoints of each path are given. (1≤s_i,e_i≤N).
Print Q 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.\