#P8291. [省选联考 2022] 学术社区
[省选联考 2022] 学术社区
Background
A warm reminder from Xiao I: There is a formal problem statement in the description, so you may skip the background. Also, please read all content except the background carefully before solving.
Xiao I is an OI contestant. But rather than saying Xiao I likes OI, it is better to say Xiao I likes the fun feature on the OJ he uses most often—FCCOJ—called the Academic Community. Although it is called an academic community, what Xiao I and other users can talk about is far more than academics. Every day, many posts appear in the academic community that catch Xiao I’s attention. Today, while browsing the community, Xiao I found a post like this:
builtin_clz: Newbie asking for help. For this Academic Community problem, it gets AC locally but RE after submission.
builtin_ctz: Belowbuiltin_clz.
jinkela: Belowbuiltin_ctz.
builtin_ctz: Abovebuiltin_clz.
builtin_clz: Can you stop being weird? Please answer the question seriously.
OrzTourist: Belowbuiltin_clz.
OrzTourist: BelowOrzTourist.
builtin_clz: Why is nobody answering? I’m angry!
builtin_clz: Abovebuiltin_clz.
builtin_clz: Belowbuiltin_clz.
builtin_clz: Abovebuiltin_clz.
builtin_clz: Belowbuiltin_clz.……
Although the user named builtin_clz got angry because nobody answered his academic question, this funny way of speaking amused Xiao I for a long time. This shows that people’s joys and sorrows are not connected. But when Xiao I refreshed the page to continue reading the replies, he found that the community admin had deleted the post because it contained too much spam.
To restore this interesting post, Xiao I dug through the web cache for a long time and recovered every message in the post. However, for mysterious reasons, the order of the messages was shuffled, and the cache did not contain the sending time of each message, so Xiao I had no way to restore the original order.
Following the spirit of “when you meet difficulties, go to sleep,” Xiao I decided to arrange the messages in any order. But since he is fascinated by the “XXX above” and “XXX below” style, he still hopes that after reordering, as many messages of this style as possible will match the real situation of the post. However, Xiao I is an OI contestant who only knows how to spam the community and cannot solve problems, so he asks you for help.
Of course, Xiao I knows that giving you the raw original messages directly is inconvenient, so he standardized the messages. See the formal statement in the description. Also, due to special rules of the academic community, the messages in the post satisfy certain special constraints. See the end of the description.
Problem Description
All string equality checks in this problem are case-sensitive. For example, 1oushang, Loushang, and LOUSHANG are all different strings.
Xiao I is organizing a post in the academic community. There are users who have posted messages, with usernames . There are messages in total. For the -th message, we describe it by a triple of strings , where is the username of the sender, and and describe the message content.
For the -th message, we define whether it is a “below-type message”, an “above-type message”, or an academic message as follows:
- If is
louxia, and is exactly equal to some given username (note that is allowed), then this message is called a below-type message, and is the user mentioned by the message. - If is
loushang, and is exactly equal to some given username (note that is allowed), then this message is called an above-type message, and is the user mentioned by the message. - If neither of the above two conditions is satisfied, then this message is an academic message.
A reordering of all messages is defined as a permutation of to , meaning that the first message is , the second message is , and so on.
For the -th message in a reordering (where ), we define whether it is consistent with the real situation as follows:
- If this message is a below-type message, then it is consistent with the real situation if and only if and , i.e., the previous message exists and its sender is exactly the user mentioned by this message.
- If this message is an above-type message, then it is consistent with the real situation if and only if and , i.e., the next message exists and its sender is exactly the user mentioned by this message.
- If this message is an academic message, then no matter what, it is never consistent with the real situation, because Xiao I only wants to spam and does not want academics.
Under the above definitions, Xiao I wants to find a reordering that maximizes the number of messages consistent with the real situation. You need to help him find such a reordering and the maximum number of consistent messages.
To make it easier for you to solve, Xiao I also tells you a special constraint of the post: because the academic community will mute people who only spam and never do academics, in the post given by Xiao I, every person who has posted in the thread must have sent at least one academic message.
Input Format
This problem has multiple test cases. The first line contains an integer denoting the number of test cases, followed by test cases.
For each test case, the first line contains two integers , denoting the number of users who have posted in the thread and the number of messages in the thread, respectively.
The next lines each contain a string , the username of a user who has posted in the thread. It is guaranteed that within each test case, the usernames are pairwise distinct.
The next lines each contain three strings describing a message. Adjacent strings are separated by one space. Here, must be equal to one of the usernames in the input.
All input strings consist only of uppercase and lowercase English letters, underscore (_), question mark (?), exclamation mark (!), and period (.), and have length at most .
For each test case, it is guaranteed that each of the usernames has sent at least one message, and has sent at least one academic message.
In the same test case, multiple messages may have exactly the same content; in this case, they should be treated as multiple messages.
Output Format
For each test case, output two lines.
The first line outputs a non-negative integer, the maximum number of messages that are consistent with the real situation among all reorderings.
The second line outputs integers , representing a reordering that achieves the maximum number of consistent messages.
If there are multiple valid reorderings, output any one of them.
2
4 15
builtin_clz
builtin_ctz
jinkela
OrzTourist
builtin_clz MengXin QiuZhu
builtin_ctz builtin_clz louxia
jinkela builtin_ctz louxia
builtin_ctz builtin_clz loushang
builtin_clz BieMoZheng YaoXueShu
OrzTourist builtin_clz louxia
OrzTourist OrzTourist louxia
builtin_clz Iam Angry!
builtin_clz builtin_clz loushang
builtin_clz builtin_clz louxia
builtin_clz builtin_clz loushang
builtin_clz builtin_clz louxia
builtin_ctz Xue Shu
jinkela Xue Shu
OrzTourist Xue Shu
1 9
builtin_clz
builtin_clz builtin_clz loushang
builtin_clz builtin_clz loushang
builtin_clz builtin_clz louxia
builtin_clz builtin_clz Loushang
builtin_clz builtin_clz LOUSHANG
builtin_clz Builtin_clz loushang
builtin_clz loushang louxia
builtin_clz builtin_clz builtin_clz
builtin_clz loushang builtin_clz
9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3
8 1 2 7 9 3 6 4 5
见附件中的 community/community2.in
见附件中的 community/community2.ans
Hint
[Sample Explanation #1]
The first test case is basically the same as the example given in the background. The difference is that, to satisfy the requirement that everyone sends at least one academic message, there are a few extra academic messages at the end of this test case.
In the second test case, the first two input messages are above-type messages, the third message is a below-type message, and the other messages are academic messages.
[Sample #3]
See community/community3.in and community/community3.ans in the attachment.
This sample satisfies special property A, special property B, and special property C in the constraints.
[Sample #4]
See community/community4.in and community/community4.ans in the attachment.
This sample satisfies special property C in the constraints.
[Constraints]
Let be the sum of over all test cases in a single test point.
For all test points, , , .
| Test point ID | Special property A | Special property B | Special property C | |||
|---|---|---|---|---|---|---|
| None | ||||||
| Yes | Yes | Yes | ||||
| No | ||||||
| No | Yes | |||||
| No | ||||||
| None | ||||||
| Yes | Yes | Yes | ||||
| No | ||||||
| No | Yes | |||||
| No | ||||||
| None | ||||||
Note: For easier reading, the test point ID is in the fourth column of the table.
Special property A: There are no above-type messages. Note: This does not mean that is not equal to loushang.
Special property B: For each test case, there exists a reordering such that every above-type message and below-type message is consistent with the real situation.
Special property C: For each test case, if there exists a message loushang, where are arbitrary strings, then in this test case there must not exist a message louxia.
[Scoring]
If, for a test point, the number of messages consistent with the real situation is correct for all test cases, you will get of the score for that test point. On this basis, if, for a test point, the reordering is also correct for all test cases, you will get the full score for that test point. Note that if you only want to get of the score, you must still output a permutation of to on the second line for each test case, otherwise your actual score may differ from your expected score.
[Reminder]
Because this may be important to you, Xiao I emphasizes again: because the academic community will mute people who only spam and never do academics, in the post given by Xiao I, every person who has posted in the thread must have sent at least one academic message.
The input size of this problem is large. Please use a relatively fast input method.
Translated by ChatGPT 5