#K46137. Social Network Simulation

    ID: 27910 Type: Default 1000ms 256MiB

Social Network Simulation

Social Network Simulation

This problem requires you to simulate an evolving social network using the union-find (also known as disjoint set union) data structure. The network consists of n users, and you will process q queries which can be one of three types:

  • add x y: Create a friendship between user x and user y. If they are already friends, ignore the operation. The union operation is based on the classic union-find algorithm with union by rank. In mathematical terms, if we denote the parent function as \(p(x)\), the union operation merges the sets containing \(x\) and \(y\) such that: \[ p(\text{root}(x)) = p(\text{root}(y)) \] after comparing the ranks.
  • remove x y: Remove the friendship between users x and y. To keep the implementation simple, when a removal occurs, the union-find structure is rebuilt from scratch using the current set of friendships.
  • largest_group: Output the size of the largest group in the network. Initially, every user is isolated, so the size is 1 if no friendship exists.

Be careful with the removal operation as it requires recomputing the union-find structure. The union-find maintains a maximum group size that is updated during each union operation.

inputFormat

The first line of input contains two integers n and q, where n is the number of users and q is the number of queries.

Each of the following q lines contains a query in one of the following formats:

  • add x y
  • remove x y
  • largest_group

For the add and remove operations, x and y are integers representing user IDs (1-indexed).

outputFormat

For every largest_group query, output a single line containing the size of the largest group in the network at that time.

## sample
6 7
add 1 2
add 2 3
largest_group
add 4 5
largest_group
remove 2 3
largest_group
3

3 2

</p>