타입이 붙은 트리에서 M개의 질의마다 두 정점 사이 경로 위에 요청한 타입의 소가 하나라도 있는지 판별한다.
어려움8트리DFS누적 합이분 탐색아직 제출이 없습니다시간 제한2초메모리 제한512 MBFarmer John is planning to build N (1≤N≤105) farms that will be connected by N−1 roads, forming a tree (i.e., all farms are reachable from each-other, and there are no cycles). Each farm contains a cow with an integer type T_i between 1 and N inclusive.
Farmer John's M friends (1≤M≤105) often come to visit him. During a visit with friend i, Farmer John will walk with his friend along the unique path of roads from farm A_i to farm B_i (it may be the case that A_i=B_i). Additionally, they can try some milk from any cow along the path they walk. Since most of Farmer John's friends are also farmers, they have very strong preferences regarding milk. Each of his friends will only drink milk from a certain type of cow. Any of Farmer John's friends will only be happy if they can drink their preferred type of milk during their visit.
Please determine whether each friend will be happy after visiting.
The first line contains two integer N and M.
The second line contains N space-separated integers T_1,T_2,…,T_N. The type of the cow in the i-th farm is denoted by T_i.
The next N−1 lines each contain two distinct integers X and Y (1≤X,Y≤N), indicating that there is an edge between farms X and Y.
The next M lines contain integers A_i, B_i, and C_i. A_i and B_i represent the endpoints of the path walked during friend i's visit, while C_i (1≤C_i≤N) indicates the type of cow whose milk the friend enjoys drinking.
Print a binary string of length M. The ith character of the string should be '1' if the ith friend will be happy, or '0' otherwise.