Friendships

시간 제한3초메모리 제한2048 MB

요약
아이들이 친구가 되고 장난감을 받는 q개의 질의가 주어지며, Q 질의마다 친구가 아닌 아이가 가진 장난감 수의 최댓값을 출력한다.
난이도

보통10점 중 7점

유형
유니온 파인드, 해시맵, 구현, 완전 탐색
정답자
아직 제출이 없습니다

문제

In the Imaginative Child's Play Classroom (ICPC) there are nn children. Over time, some of the children become friends. Friendship is a two way street, so if child A is a friend of child B, then child B is a friend of child A. On the other hand, friendship is not transitive. If child A is a friend with child B, and child B is friends with child C, then child A might not be friends with child C. Since its the start of a new school year, none of the children are friends with each other yet.

If a child behaves well in the classroom, the teacher will sometimes give the child a toy. Initially none of the children have any toys. The ICPC thinks that no child should have more than 5050 toys, so teachers are not allowed to give a toy to a child if they would exceed 5050 toys.

Sometimes, a child will get bored of playing with toys with their friends and would be jealous of other children that have lots of toys. The child will wonder what the maximum number of toys that a child who is not their friend has.

입력

The first line contains two integers: the number of children nn, and a number of queries qq (1≤n,q≤4⋅1051 \le n,q \le 4 \cdot 10^5).

The children are numbered from 11 to nn. Each query is of the following form:

  • F i j -- indicating that child ii became friends with child jj. It is guaranteed that child ii is not already friends with child jj, and i≠ji \neq j.
  • A i -- indicating that the teacher has given a toy to child ii.
  • Q j -- indicating that the child jj would like to know the maximum number of toys any other child who is not their friend has.

It is guaranteed that no child will ever have more than 5050 toys.

출력

For every query of the form Q j, output a single integer kk, which is the maximum number of toys any other child who is not a friend of child jj has. If child jj is friends with all the other children, let k=−1k=-1.

예제1

  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