#P14690. [ICPC 2025 Yokohama R] Membership Structure of a Secret Society

[ICPC 2025 Yokohama R] Membership Structure of a Secret Society

题目描述

A secret society with an undisclosed name was established in the year 1899 by a single founder, whose name is also kept secret. Subsequent members have joined the society through recommendations of existing members.

One unique rule for joining the society has been strictly enforced: Recommendations can be made by one or more existing members, but the same member group can recommend only one new member. For example, if a member was allowed to join upon the recommendation by a group of existing members {A,B,C}\{A, B, C\}, no other persons can be allowed by the same recommender group. It is perfectly acceptable, however, for a group {A,B}\{A, B\} to recommend another new member. Although the set {A,B}\{A, B\} is a subset of {A,B,C}\{A, B, C\}, they are distinct sets. For consistency, the group of recommenders of the founder is considered to be the empty set.

Through investigation of this secret society, you have obtained several information fragments representing some part of the membership structure of the society. Each information fragment takes one of the following forms of statements, in which the symbols aa, bb and cc are integers designating certain members of the society.

  • recommend aa bb
    meaning that the member aa belongs to the group that recommended the member bb to join.

  • not-recommend aa bb
    meaning that the member aa does not belong to the group that recommended the member bb to join.

  • intersection aa bb cc
    meaning that the group of the recommenders of the member aa is the set intersection of recommender group of bb and that of cc. In other words, all of those who recommended aa also recommended both bb and cc, and all of those who recommended both bb and cc also recommended aa.

Two or more occurrences of the same integer mean the same member, even in different statements. On the other hand, two or more different integers may be aliases of the same person, even in a single statement.

The obtained information may be partial, that is, the recommendations of some members may be missing, and, moreover, there may be some members not mentioned in any of the statements.

As the information sources are not necessarily reliable, some false information might have crept in. You would like to know whether these statements are consistent, that is, whether there can be a recommendation relationship consistent with all of these statements.

输入格式

The input contains one or more test cases. The first line of the input contains an integer tt (1t30001 \le t \le 3000), which is the number of test cases. The descriptions of the tt test cases follow, each in the following format.

nn s1s_1 \vdots sns_n

The first line contains a single integer nn, the number of statements (1n30001 \le n \le 3000). Each of the following nn lines is in either of the formats "recommend aa bb", "not-recommend aa bb", or "intersection aa bb cc", with all of aa, bb, and cc being integers between 1 and 3n3n, inclusive.

The sum of nn's over all the test cases does not exceed 3000.

输出格式

For each test case, output yesyes in one line if the situation described in the statements is possible, and output nono, otherwise.

3
2
recommend 1 2
not-recommend 1 2
2
recommend 1 2
recommend 2 1
3
intersection 1 2 2
recommend 1 3
not-recommend 2 3
no
no
no