#P8291. [省选联考 2022] 学术社区

    ID: 9374 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>各省省选2022网络流Special JudgeO2优化

[省选联考 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: Below builtin_clz.

jinkela: Below builtin_ctz.

builtin_ctz: Above builtin_clz.

builtin_clz: Can you stop being weird? Please answer the question seriously.

OrzTourist: Below builtin_clz.

OrzTourist: Below OrzTourist.

builtin_clz: Why is nobody answering? I’m angry!

builtin_clz: Above builtin_clz.

builtin_clz: Below builtin_clz.

builtin_clz: Above builtin_clz.

builtin_clz: Below builtin_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 NN users who have posted messages, with usernames n1,n2,,nNn_1, n_2, \ldots, n_N. There are MM messages in total. For the ii-th message, we describe it by a triple of strings (si,1,si,2,si,3)(s_{i,1}, s_{i,2}, s_{i,3}), where si,1s_{i,1} is the username of the sender, and si,2s_{i,2} and si,3s_{i,3} describe the message content.

For the ii-th message, we define whether it is a “below-type message”, an “above-type message”, or an academic message as follows:

  • If si,3s_{i,3} is louxia, and si,2s_{i,2} is exactly equal to some given username (note that si,2=si,1s_{i,2} = s_{i,1} is allowed), then this message is called a below-type message, and si,2s_{i,2} is the user mentioned by the message.
  • If si,3s_{i,3} is loushang, and si,2s_{i,2} is exactly equal to some given username (note that si,2=si,1s_{i,2} = s_{i,1} is allowed), then this message is called an above-type message, and si,2s_{i,2} 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 a1,a2,a3,,aMa_1, a_2, a_3, \ldots, a_M of 11 to MM, meaning that the first message is (sa1,1,sa1,2,sa1,3)(s_{a_1,1}, s_{a_1,2}, s_{a_1,3}), the second message is (sa2,1,sa2,2,sa2,3)(s_{a_2,1}, s_{a_2,2}, s_{a_2,3}), and so on.

For the ii-th message (sai,1,sai,2,sai,3)(s_{a_i,1}, s_{a_i,2}, s_{a_i,3}) in a reordering a1,a2,a3,,aMa_1, a_2, a_3, \ldots, a_M (where 1iM1 \le i \le M), 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 i1i \ne 1 and sai1,1=sai,2s_{a_{i - 1}, 1} = s_{a_i, 2}, 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 iMi \ne M and sai+1,1=sai,2s_{a_{i + 1}, 1} = s_{a_i, 2}, 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 TT denoting the number of test cases, followed by TT test cases.

For each test case, the first line contains two integers N,MN, M, denoting the number of users who have posted in the thread and the number of messages in the thread, respectively.

The next NN lines each contain a string nn, the username of a user who has posted in the thread. It is guaranteed that within each test case, the NN usernames are pairwise distinct.

The next MM lines each contain three strings s1,s2,s3s_1, s_2, s_3 describing a message. Adjacent strings are separated by one space. Here, s1s_1 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 1212.

For each test case, it is guaranteed that each of the NN 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 MM integers a1,a2,,ama_1, a_2, \ldots, a_m, 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 M\sum M be the sum of MM over all test cases in a single test point.

For all test points, 1T1001 \le T \le 100, 1NM777771 \le N \le M \le 77777, 1M2.5×1051 \le \sum M \le 2.5 \times {10}^5.

TT \le MM \le M\sum M \le Test point ID Special property A Special property B Special property C
55 1010 5050 11 None
1010 1616 160160 22
3030 22222222 1500015000 343 \sim 4 Yes Yes Yes
565 \sim 6 No
797 \sim 9 No Yes
101110 \sim 11 No
121312 \sim 13 None
100100 7777777777 2.5×1052.5 \times {10}^5 141514 \sim 15 Yes Yes Yes
1616 No
171917 \sim 19 No Yes
202220 \sim 22 No
232523 \sim 25 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 s3\bm{s_3} 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 s1s_1 s2s_2 loushang, where s1,s2s_1, s_2 are arbitrary strings, then in this test case there must not exist a message s2s_2 s1s_1 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 50%50 \% 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 50%\bm{50 \%} of the score, you must still output a permutation of 1\bm{1} to M\bm{M} 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