50104. Students and Clubs

I'm a slow walker, but I never walk backwards.

Write a program to record club information and answer queries.

There are at most $N$ clubs for $M$ students to create or join. Each student can create or join as many clubs as he wishes. All students and clubs have their own names. We record club information according to a sequence of instructions. Each instruction consists of a number (0 or 1) and two strings. The first string is the student name, and the second string is the club name.

• If the instruction is 0 A B, indicating that student $A$ creates club $B$.
• If the instruction is 1 A B, indicating that student $A$ joins club $B$.

For example, if the instruction is 0 Sara Tennis. It means that Sara creates a club named "Tennis", and she is not only the member but also the leader of Tennis club. If the instruction is 1 John Dance, it means that John joins the club "Dance", and becomes a member of Dance club.

After students join the clubs, you need to answer a series of queries code. There are two kinds of queries. Each query starts with a number either 0 or 1.

• The first kind of query consists of a 0 and a string for a club name. For example, 0 A is a query asking for the name of leader the club $A$.
• The answer should be a string for the name of the leader if club $A$ exists.
• The answer should be "None" if club $A$ does not exist.
• The second kind of query consists of a 1 and two strings. The first string is a student name, and the second string is a club name. For example, 1 A B is a query asking if the student $A$ is a member or the leader of club $B$.
• If club $B$ exists then return the following. If $A$ is a member or the leader of club $B$ the answer is $1$; otherwise, the answer is $0$.
• If club $B$ does not exist the answer should be $-1$.

We use the previous 0 Sara Tennis and 1 John Dance as examples. Now if the query is 0 Tennis, the answer is "Sara" since Sara is the leader of Tennis club. If the query is 1 Sara Dance, the answer is 0 since Sara is not a member of Dance club. If the query code is 1 Angela Dance, the answer is 0 since we cannot find "Angela" in the Dance club.

Write a program to record the clubs information and answer queries. It is guaranteed that a club will be created before other students can join it. And no two clubs have the same name, and a club will be created only once.

• 20 points: Each club has only the leader and no other members.
• 20 points: There is only one club.
• 60 points: Nothing is guaranteed. Note that you will get TLE if you put information in an array and search through it to answer queries.

Input Format

The input contains only one test case. The first line contains an integer $K$, where $K$ is the number of instructions. For the following $K$ lines, each line contains one instruction. The line after $K+1$ lines contains an integer $Q$, where $Q$ is the number of inquiries. For the following $Q$ lines, each line contains one query code.

• $K \le 100000, Q \le 100000$.
• $N \le 5000, M \le 1000$.
• Name length is no longer than 40.

Output Format

Print the results of a series of inquiries. Each line contains a result.

Sample Input 1

50 Sara Tennis0 John Dance0 Una Singing0 Rex Badminton0 C TESTEST60 Tennis1 John Tennis1 Una Singing1 Rex TESTEST0 CCC1 JJJ TESTEST


Sample Output 1

Sara010None0


Sample Input 2

50 Sara Tennis1 John Tennis1 Una Tennis1 Rex Tennis1 C Tennis60 Singing1 John Tennis1 Una Singing0 Tennis1 Rex Tennis 1 JJJ Tennis


Sample Output 2

None1-1Sara10


Sample Input 3

90 Sara Tennis1 John Tennis0 John Dance1 Una Tennis0 Una Singing0 Rex Badminton0 C TESTEST1 Rex Tennis1 C Tennis80 Tennis1 John Tennis1 Una Singing1 Rex TESTEST0 Singing1 Kay TESTEST1 C TESTEST0 Rex


Sample Output 3

Sara110Una01None


Note

The binary search tree sample code: tree.c

To process a large amount of queries, you need to build a binary search tree for all clubs. Each tree node will have the name of the club and the name of its leader. By using a binary search tree, one can easily check if a given club exists. In addition, from each club node you also need to build a binary search tree for all members of this club, so that you can easily check if a student is in that club. That is, each node of this tree contains a student name. Consequently, the tree for clubs should be arranged by the club names, and the tree for club members should be arranged by the student names.