pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

Students and Clubs #358

Open littlehug opened 5 years ago

littlehug commented 5 years ago

Write a program to record club information and answer queries.

Task Description

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.

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.

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.

Subtask

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.

Output Format

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

Sample Input 1

5
0 Sara Tennis
0 John Dance
0 Una Singing
0 Rex Badminton
0 C TESTEST
6
0 Tennis
1 John Tennis
1 Una Singing
1 Rex TESTEST
0 CCC
1 JJJ TESTEST

Sample Output 1

Sara
0
1
0
None
0

Sample Input 2

5
0 Sara Tennis
1 John Tennis
1 Una Tennis
1 Rex Tennis
1 C Tennis
6
0 Singing
1 John Tennis
1 Una Singing
0 Tennis
1 Rex Tennis 
1 JJJ Tennis

Sample Output 2

None
1
-1
Sara
1
0

Sample Input 3

9
0 Sara Tennis
1 John Tennis
0 John Dance
1 Una Tennis
0 Una Singing
0 Rex Badminton
0 C TESTEST
1 Rex Tennis
1 C Tennis
8
0 Tennis
1 John Tennis
1 Una Singing
1 Rex TESTEST
0 Singing
1 Kay TESTEST
1 C TESTEST
0 Rex

Sample Output 3

Sara
1
1
0
Una
0
1
None

Note

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.