#P5838. [USACO19DEC] Milk Visits G

[USACO19DEC] Milk Visits G

Description

Farmer John plans to build NN farms and connect them with N1N-1 roads to form a tree (that is, every pair of farms is reachable, and there are no cycles). Each farm has one cow, with breed TiT_i, an integer between 11 and NN.

Farmer John has MM friends who often come to visit him. When friend ii visits, Farmer John and this friend will walk along the unique path from farm AiA_i to farm BiB_i (it is possible that Ai=BiA_i = B_i). In addition, they can taste the milk of any cow on the path they travel. Since most of Farmer John’s friends are also farmers, they have strong preferences for milk. Each friend only drinks milk from one specific breed of cow. A friend will be happy only if, during their visit, they can drink milk of their preferred breed.

Please determine whether each friend will be happy after the visit.

Input Format

The first line contains two integers NN and MM.

The second line contains NN space-separated integers T1,T2,,TNT_1,T_2,\ldots, T_N. The cow breed on farm ii is given by TiT_i.

The next N1N-1 lines each contain two distinct integers XX and YY (1X,YN1 \leq X, Y \leq N), indicating there is an edge between farms XX and YY.

The next MM lines each contain integers AiA_i, BiB_i, and CiC_i. AiA_i and BiB_i are the endpoints of the path friend ii walks during the visit, and CiC_i (1CiN1 \le C_i \le N) is the cow breed whose milk this friend likes.

Output Format

Output a binary string of length MM. If friend ii will be happy, the ii-th character of the string is 1, otherwise it is 0.

5 5
1 1 2 1 2
1 2
2 3
2 4
1 5
1 4 1
1 4 2
1 3 2
1 3 1
5 5 1
10110
6 4
1 2 3 3 3 3
1 2
2 3
3 4
2 5
5 6
4 6 1
4 6 2
4 6 3
4 6 4
0110

Hint

Test point properties.

Test point 22 is the second sample below.

Test point 33 satisfies N103N\le 10^3, M2103M\le 2\cdot 10^3.

Test points 474\sim 7 satisfy Ci10C_i\le 10.

For 100%100\% of the testdata, 1N1051 \leq N \leq 10^5, 1M1051 \leq M \leq 10^5.

Problem author: Spencer Compton.

Translated by ChatGPT 5