#P5836. Milk Visits on the Farm Tree
Milk Visits on the Farm Tree
Milk Visits on the Farm Tree
Farmer John plans to build farms connected by roads forming a tree, so every farm can reach any other farm without any cycles. Each farm houses a cow which is either a Guernsey (denoted by 'G') or a Holstein (denoted by 'H').
Farmer John's friends visit him. For the -th friend, he travels along the unique path from farm to farm (note that and might be the same). During the visit, the friend can taste the milk of any cow along the path. Each friend has a strong preference and will only drink milk from a specific breed – some only drink Guernsey milk while others only drink Holstein milk. A friend is happy if and only if their preferred type of cow appears on the path they traverse.
inputFormat
The first line contains two integers and , representing the number of farms and the number of friends respectively.
The second line is a string of length consisting only of characters 'G' and 'H'. The -th character represents the type of cow at farm .
Each of the next lines contains two integers and , indicating that there is a road connecting farm and farm .
The following lines each contain two integers , and a character (either 'G' or 'H'), representing the starting farm, ending farm, and the friend’s preferred cow type respectively.
outputFormat
Output a binary string of length . The -th character of the string should be '1' if the -th friend is happy (i.e. if the path from to contains at least one cow of type ) and '0' otherwise.
sample
4 3
GGGH
1 2
2 3
3 4
1 4 G
1 4 H
2 3 H
110
</p>