Quick Answer

Time limit1sMemory limit128 MB

Summary
Simulate union-find with a special disconnect operation that isolates a node while keeping the rest of its group connected, then answer connectivity queries.
Level

Medium6 of 10

Topics
Union-find, Implementation, Simulation
Solved
No attempts yet

Problem

Joe is fond of computer games. He is now facing a large map dotted with fortified towns. His opponent can issue commands that connect or disconnect towns.

Think of the towns as being organized into groups. Two towns are connected exactly when they belong to the same group. Initially every town sits alone in its own group and is isolated.

  • A connect command merges the two groups that contain the two named towns into a single group. So connecting to any one town also connects you to every town already in that town's group.
  • A disconnect command takes a single named town out of its current group, isolating it again. The other towns that shared its group stay connected to one another. The removed town keeps no memory of its former connections, so it must be reconnected explicitly.

Between the opponent's commands, questions of the form "is town ii connected with town jj?" appear. Write a program that counts how many questions are answered "yes" and how many are answered "no".

Input

The input may contain several data sets; each data set describes one map together with its commands, in the following form:

  • The first line holds the number of towns NN (N≤10000N \le 10000).
  • Then each command appears on its own line:
    • c i j — merge the groups containing towns ii and jj (1≤i,j≤N1 \le i, j \le N).
    • d i — remove town ii from its group, isolating it (1≤i≤N1 \le i \le N).
    • q i j — the question: is town ii connected with town jj? (1≤i,j≤N1 \le i, j \le N).
    • e — marks the end of the current data set's command list.

The c, d, and q commands may appear in any order, and each command is processed against the current configuration. Several data sets may follow one another until the end of the file.

Output

For each data set, print on one line the number of questions answered "yes", N1N_1, and the number answered "no", N2N_2. Write the two numbers in the form N_1 , N_2, separated by a space, a comma, and a space (,). For instance, the data set in the example below has 2 "yes" answers and 2 "no" answers, so it prints 2 , 2.

Examples1

  1. Example 1

    Input
    4
    c 1 2
    c 3 4
    q 1 3
    c 2 3
    q 1 4
    d 2
    q 4 1
    q 2 4
    e
    
    Expected output
    2 , 2