#P12502. 「CEOI2025」BoardOina 游戏

「CEOI2025」BoardOina 游戏

Description

Every year, a big Boardgame Expo takes place in Cluj-Napoca, showcasing a wide selection of new games. The main attraction this year is a game called BoardOina.

There are nn players lined up in a queue, waiting to try out the game. Players are numbered from 00 to n1n-1 in their order in the queue. Player 00 is at the front of the queue and player n1n-1 is at the back.

There are mm distinct friendship relations between mm pairs of players in the queue. Specifically, for each ii from 00 to m1m-1, inclusive, player x[i]x[i] and player y[i]y[i] are friends, where 0x[i]<y[i]<n0 \leq x[i] < y[i] < n. Friendship relations are symmetric.

Consider a sequence of kk consecutive players in the queue starting at player ss (for any ss and kk such that 0s<n0 \leq s < n and 1kns1 \leq k \leq n-s). This sequence of players forms a friend group of size kk if for all pairs of two players, they are connected by a sequence of friendship relations within that friend group. Specifically, players s,s+1,,s+k1s, s+1, \ldots, s+k-1 form a friend group of size kk if, for each uu and vv such that su<v<s+ks \leq u < v < s+k, there exists a sequence of players p[0],,p[l1]p[0], \ldots, p[l-1] such that:

  • l2l \geq 2;
  • sp[j]<s+ks \leq p[j] < s+k for each jj from 00 to l1l-1, inclusive;
  • p[0]=up[0] = u and p[l1]=vp[l-1] = v;
  • players p[j]p[j] and p[j+1]p[j+1] are friends for each jj from 00 to l2l-2, inclusive.

Note that in the case of k=1k = 1, player ss alone forms a friend group of size 11.

BoardOina can be played by any number of players. However, to make the game more successful, the organizers only let friend groups play it.

Only one group can play at a time. For each game, a friend group starting at the player at the front of the queue is formed, and starts playing the game. The players in this friend group are removed from the queue. This process is repeated until the queue becomes empty. Formally, we say that the queue can be partitioned into gg friend groups if there exists an array of group sizes, K=[K[0],K[1],,K[g1]]K = [K[0], K[1], \ldots, K[g-1]], such that each of the following conditions holds.

  • g>0g > 0 and K[j]>0K[j] > 0 (for each jj such that 0j<g0 \leq j < g);
  • K[0]+K[1]++K[g1]=nK[0] + K[1] + \ldots + K[g-1] = n;
  • for each jj between 00 and g1g-1, inclusive, players s[j],s[j]+1,,s[j]+K[j]1s[j], s[j]+1, \ldots, s[j]+K[j]-1 form a friend group of size K[j]K[j], where s[0]=0s[0]=0 and otherwise s[j]=K[0]+K[1]++K[j1]s[j]=K[0]+K[1]+\ldots+K[j-1].

The organizers want to minimize the number of friend groups that play the game. That is, they want to partition the queue into gg friend groups such that it is not possible to partition the queue into g1g-1 (or less) friend groups.

Your task is to find a partitioning of the queue into a minimum number of friend groups, and report the array of group sizes.

Implementation Details

You should implement the following procedure.

std::vector<int> partition_players(int n, int m, std::vector<int> X, std::vector<int> Y)
  • n: the number of players in the queue.
  • m: the number of friendship relations.
  • X, Y: arrays of length mm describing friendship relations.
  • This procedure should return an array of group sizes, representing a partition of the player queue into a minimum number of friend groups.
  • This procedure is called exactly once for each test case.

Constraints

  • 2n1000002 \leq n \leq 100\,000
  • 0m2000000 \leq m \leq 200\,000
  • 0x[i]<y[i]<n0 \leq x[i] < y[i] < n (for each ii such that 0i<m0 \leq i < m)
  • Friendship relations are distinct. In other words, x[i]x[j]x[i] \neq x[j] or y[i]y[j]y[i] \neq y[j] (for each ii and jj such that 0i<j<m0 \leq i < j < m)
  • If there are multiple solutions with minimum number of groups, you can return any valid solution.

Subtasks

  1. (5 points) y[i]=x[i]+1y[i] = x[i] + 1 for each ii from 00 to m1m-1, inclusive.
  2. (7 points) y[i]x[i]+2y[i] \leq x[i] + 2 for each ii from 00 to m1m-1, inclusive.
  3. (6 points) n300n \leq 300 and m600m \leq 600
  4. (15 points) n2000n \leq 2000 and m4000m \leq 4000
  5. (34 points) There are no friendship relations which are cyclic. That is, for any sequence of distinct players p[0],p[1],,p[l1]p[0], p[1], \ldots, p[l-1], such that l3l \geq 3 and for each 0j<l10 \leq j < l-1 players p[j]p[j] and p[j+1]p[j+1] are friends, players p[0]p[0] and p[l1]p[l-1] are not friends.
  6. (33 points) No additional constraints.

Sample 1

Consider the following call:

partition_players(5, 3, {0, 1, 3}, {1, 4, 4})

In this example, players 00 and 11, players 11 and 44, and players 33 and 44 are friends.

Player 22 has no friends in the queue, hence there must be a friend group formed by player 22 alone, which means that the minimum number of friend groups is g=3g = 3. On the other hand, players 00 and 11, as well as players 33 and 44 can form a friend group of size 22.

Therefore, the queue can be partitioned into 33 friend groups of sizes 22, 11 and 22, so the procedure may return [2, 1, 2].

Sample 2

Consider the following call:

partition_players(7, 6, {0, 4, 2, 1, 2, 3}, {1, 5, 4, 5, 5, 6})

In this example, players 00 and 11, players 44 and 55, players 22 and 44, players 11 and 55, players 22 and 55 and players 33 and 66 are friends.

The only friend of player 33 is player 66, so any friend group containing player 33 is either

  • a friend group of size 11 containing player 33 alone, or
  • a friend group containing both player 33 and player 66.

A friend group in the second case must also contain players 44 and 55. This is not possible as the only friend of player 66 is player 33, so player 33 is not connected to players 44 and 55 by a sequence of friendship relations.

Therefore, player 33 must be placed in a friend group of size 11. Similarly, player 66 must also be placed in a friend group of size 11, therefore the number of friend groups in a partition is at least 44.

Players 00, 11 and 22 do not form a friend group of size 33, as neither player 00 or player 11 is connected to player 22 by a sequence of friendship relations within the group. That would have not been the case if 55 was in the group, but since 33 and 44 will definitely be in different groups, that will never happen. That is, the number of friend groups in a partition is at least 55.

On the other hand, players 00 and 11, and players 44 and 55 form two friend groups of size 22.

Therefore, the queue can be partitioned into 55 friend groups of sizes 22, 11, 11, 22 and 11. The procedure may return [2, 1, 1, 2, 1].

Sample Grader

The sample grader reads the input in the following format:

  • line 11: n mn\ m
  • line 2+i2 + i (0i<m0 \leq i < m): x[i] y[i]x[i]\ y[i]

Let the elements of the array returned by partition_players be K[0],K[1],,K[g1]K[0], K[1], \ldots, K[g-1] for some nonnegative gg. The output of the sample grader is in the following format:

  • line 11: gg
  • line 22: K[0] K[1]  K[g1]K[0]\ K[1]\ \ldots\ K[g-1]