A247.[USACO 2019 December Silver]Milk Visits

普及/提高-

USACO

通过率:0%

时间限制:1.00s

内存限制:128MB

题目描述

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 Ai to farm Bi (it may be the case that Ai=Bi). 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. Some of his friends will only drink Guernsey milk, while the remainder will only drink Holstein milk. 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.

SCORING:
Test cases 2-5 satisfy N≤103,M≤2⋅103.
INPUT FORMAT:
The first line contains the two integers N and M.
The second line contains a string of length N. The ith character of the string is 'G' if the cow in the ith farm is a Guernsey, or 'H' if the cow in the ith farm is a Holstein.

The next N−1 lines each contain two distinct integers X and Y (1≤X,Y≤N), indicating that there is a road between farms X and Y.

The next M lines contain integers Ai, Bi, and a character Ci. Ai and Bi represent the endpoints of the path walked during friend i's visit, while Ci is either G or H if the ith friend prefers Guernsey milk or Holstein milk.

OUTPUT FORMAT:
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.

Farmer John 计划建造 NN(1≤N≤105)10^5)个农场,用 N1N−1 条道路连接,构成一棵树(也就是说,所有农场之间都互相可以到达,并且没有环)。每个农场有一头奶牛,品种为更赛牛或荷斯坦牛之一。

Farmer John 的 MM 个朋友(1≤M≤10510^5)经常前来拜访他。在朋友 ii 拜访之时,Farmer John 会与他的朋友沿着从农场 AiA_i 到农场 BiB_i 之间的唯一路径行走(可能有 Ai=BiA_i=B_i)。除此之外,他们还可以品尝他们经过的路径上任意一头奶牛的牛奶。由于 Farmer John 的朋友们大多数也是农场主,他们对牛奶有着极强的偏好。他的有些朋友只喝更赛牛的牛奶,其余的只喝荷斯坦牛的牛奶。任何 Farmer John 的朋友只有在他们访问时能喝到他们偏好的牛奶才会高兴。

请求出每个朋友在拜访过后是否会高兴。

输入格式

输入的第一行包含两个整数 NNMM

第二行包含一个长为 NN 的字符串。如果第 ii 个农场中的奶牛是更赛牛,则字符串中第 ii 个字符为 G'G',如果第 ii 个农场中的奶牛是荷斯坦牛则为 H'H'

以下$ N−1$ 行,每行包含两个不同的整数 XXYY 1X,YN(1≤X,Y≤N),表示农场 XXYY 之间有一条道路。

以下 MM 行,每行包含整数 AiA_iBiB_i,以及一个字符 CiC_iAiA_iBiB_i 表示朋友 ii 拜访时行走的路径的端点,CiC_i 是 'G' 或 'H' 之一,表示第 ii 个朋友喜欢更赛牛的牛奶或是荷斯坦牛的牛奶。

输出格式

输出一个长为 MM 的二进制字符串。如果第 ii 个朋友会感到高兴,则字符串的第 ii 个字符为 1'1',否则为 0'0'

输入输出样例

  • 输入#1

    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

    输出#1

    10110

说明/提示

测试点 252-5 满足 NN10310^3,MM2210310^3

在这里,从农场 1 到农场 4 的路径包括农场 1、2 和 4。所有这些农场里都是荷斯坦牛,所以第一个朋友会感到满意,而第二个朋友不会。

首页