#P5836. [USACO19DEC] Milk Visits S
[USACO19DEC] Milk Visits S
Problem Description
Farmer John plans to build farms and connect them with 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 friends who often come to visit him. When friend visits, Farmer John and this friend will walk along the unique path from farm to farm (it is possible that ). 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 and .
The second line contains a string of length . If the cow on farm is a Guernsey cow, then the -th character is G; if the cow on farm is a Holstein cow, then the -th character is H.
The next lines each contain two distinct integers and (), indicating that there is a road between farms and .
The next lines each contain integers , , and a character . and are the endpoints of the path that friend walks during the visit. is one of G or H, indicating whether friend likes Guernsey milk or Holstein milk.
Output Format
Output a binary string of length . If friend will be happy, then the -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 is the sample.
Test points satisfy , .
For of the testdata, , .
Problem by: Spencer Compton.
Translated by ChatGPT 5