cswork / OAOpen practice workspace

DoorDashOA #5

Get Sizes of Friends Groups

Estimated MediumJudge-ready

Topics (AI-labelled)Union-Find

Initially, each person is in a single-person group. F a b merges the groups of the two people; T a b adds the sizes of the two people's groups to a running total. If the two people are already in the same group, the group size is taken separately for each of the two people as the statement says, so it is counted twice. Output the total over all T operations.

Input

n q≤100000, followed by q lines, each with F/T and two ID values in 1..n.

Output

Output the accumulated value, using a 64-bit integer.

Example 1

Input:

4 3
F 1 2
F 2 3
T 1 4

Output:

4

Example 2

Input:

2 1
T 1 1

Output:

2

Example 3

Input:

3 4
F 1 2
T 1 2
F 1 2
T 1 3

Output:

7

Editorial

Loading solution…