#P5836. [USACO19DEC] Milk Visits S

    ID: 6588 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>树形数据结构2019USACO并查集最近公共祖先 LCA

[USACO19DEC] Milk Visits S

Problem Description

Farmer John plans to build NN farms and connect them with N1N-1 roads, forming a tree (that is, all farms are reachable from each other, and there are no cycles). Each farm has one cow, whose breed is either Guernsey or Holstein.

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). Also, they can taste the milk from any cow on the path they travel. Since most of Farmer John’s friends are also farmers, they have a very strong preference for milk. Some friends only drink Guernsey milk, and the rest only drink Holstein milk. A friend will be happy only if, during their visit, they can drink the type of milk they prefer.

For each friend, determine whether they will be happy after the visit.

Input Format

The first line contains two integers NN and MM.

The second line contains a string of length NN. If the cow on farm ii is a Guernsey cow, then the ii-th character is G; if the cow on farm ii is a Holstein cow, then the ii-th character is H.

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

The next MM lines each contain integers AiA_i, BiB_i, and a character CiC_i. AiA_i and BiB_i are the endpoints of the path that friend ii walks during the visit. CiC_i is one of G or H, indicating whether friend ii likes Guernsey milk or Holstein milk.

Output Format

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

5 5
HHGHG
1 2
2 3
2 4
1 5
1 4 H
1 4 G
1 3 G
1 3 H
5 5 H
10110

Hint

Here, the path from farm 1 to farm 4 includes farms 1, 2, and 4. All these farms have Holstein cows, so the first friend will be satisfied, while the second friend will not.

About subtasks:

Test point 11 is the sample.

Test points 252\sim 5 satisfy N103N \le 10^3, M2103M \le 2\cdot 10^3.

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

Problem by: Spencer Compton.

Translated by ChatGPT 5