#P17384. [PacNW 2025] Friendships

[PacNW 2025] Friendships

Problem Description

There are nn children in the Imaginative Child's Play Classroom (ICPC). Friendships are bidirectional but not transitive. At the beginning of the school year, no two children are friends.

Teachers sometimes give toys to well-behaved children. Initially, no child has any toys. No child may ever receive more than 5050 toys.

At times, a child wants to know the maximum number of toys owned by any other child who is not their friend. Process friendship, toy, and query events as they occur.

Input Format

The first line contains two integers nn and qq (1n,q41051\le n,q\le4\cdot10^5), the number of children and the number of events. Children are numbered 11 through nn.

Each of the next qq lines has one of the following forms:

  • F i j: children ii and jj become friends. They were not already friends, and iji\ne j.
  • A i: child ii receives one toy.
  • Q j: child jj asks for the maximum number of toys owned by another child who is not their friend.

No child will ever have more than 5050 toys.

Output Format

For every Q j event, output the requested maximum. If child jj is friends with every other child, output 1-1.

3 10
Q 1
A 2
A 1
A 2
Q 3
Q 2
F 2 3
Q 3
F 1 3
Q 3
0
2
1
1
-1