#P16321. [语言月赛 202604] 生日邀请 (Hard ver.)
[语言月赛 202604] 生日邀请 (Hard ver.)
Background
This problem differs from the Easy ver. only in the constraints.
The regular version of this problem is in the beginner problem set.
Problem Description
Alice’s birthday is coming soon, and she invited many good friends to her birthday party. All friends’ names consist only of uppercase and lowercase letters.
However, her friends are all busy and are not sure whether they can attend, so they cannot give her a clear answer. The replies she receives can be divided into the following three types (where are any names):
A=>B: If attends, then also attends.A<=B: If attends, then also attends.A<=>B: If attends, then also attends, and vice versa.
She can concatenate these replies. For example, she uses Andrea=>Bob<=Cindy to mean:
- If Andrea attends, then Bob will definitely attend.
- If Cindy attends, then Bob will definitely attend as well.
She found that all her friends’ replies can form a string , and contains no repeated names.
Alice tells you this string, and then she will ask you questions. Each time she gives you two names , and she wants to know whether, if attends the birthday party, then must attend.
Input Format
The first line contains a string , representing the string formed by all friends’ replies.
The second line contains a positive integer , representing the number of queries.
The next lines each contain two names , representing one query.
Output Format
For each query, output one line with a string: if “if attends, then must attend” is true, output Yes; otherwise output No.
Andrea=>Bob<=Cindy<=>Dora
7
Andrea Bob
Dora Cindy
Dora Bob
Andrea Cindy
Andrea Mike
Mike Andrea
Mike Mike
Yes
Yes
Yes
No
No
No
Yes
Hint
[Sample 1 Explanation]
We explain each query in order:
- From
Andrea=>Bob, if Andrea attends, then Bob attends. - From
Cindy<=>Dora, if Dora attends, then Cindy attends. - If Dora attends, then Cindy will attend, which also means Bob will attend. Therefore, if Dora attends, then Bob attends.
- For the -th query, we can construct a counterexample where “only Andrea and Bob attend”. Therefore, if Andrea attends, Cindy does not necessarily attend.
- For the -th and -th queries, since we do not know any reply about Mike, the answer is clearly not guaranteed.
- But the -th query is different: “if Mike attends, then Mike must attend” is a meaningless statement, and it is always true.
[Constraints]
For all testdata, it is guaranteed that:
- consists of non-repeated names, and the separator between names must be one of
<=,=>, and<=>. - Each name consists only of English letters, with the first letter uppercase and the other letters lowercase, and none of these names is
Alice. - , and the sum of name lengths appearing in all queries is .
Translated by ChatGPT 5