#P7946. Maximum Points in Cirno's Graph Game

    ID: 21130 Type: Default 1000ms 256MiB

Maximum Points in Cirno's Graph Game

Maximum Points in Cirno's Graph Game

Cirno and Daiyousei are playing a game on a special graph with 4·n nodes numbered from 0 to 4·n - 1. The graph is constructed as follows:

  • For each i with 0 ≤ i ≤ 3 and for every 0 ≤ j, k < n, the nodes n·i + j and n·i + k are connected (i.e. each group of n consecutive nodes is a clique).
  • For each i with 0 ≤ i < n and for every 0 ≤ j, k ≤ 3, the nodes i + n·j and i + n·k are connected (i.e. the nodes in positions with the same remainder when divided by n form a clique).

The game is played as follows:

  1. Cirno selects exactly 2·n of the nodes and colors them blue. The remaining 2·n nodes remain red.
  2. Then the game proceeds in 2·n turns. In each turn, Cirno chooses one blue node (each blue node is chosen exactly once) and then Daiyousei picks one red node (each red node is chosen exactly once). If the blue and the red node chosen in that turn are connected by an edge in the graph, Daiyousei earns a point.

Daiyousei wants to maximize the number of points she gets. Given the value of n and the list of blue node indices (of size 2·n), determine the maximum number of points that Daiyousei can achieve by pairing the blue nodes with the red nodes optimally across the turns. Note that an edge exists between a blue node b (with row r = b//n and column c = b mod n) and a red node r (with row r//n and column r mod n) if and only if either they lie in the same horizontal block (i.e. same row) or in the same vertical group (i.e. same column).

Your task is to compute this maximum matching between blue and red nodes in the bipartite graph where an edge exists if they share the same row or the same column.

inputFormat

The first line contains one integer n (1 ≤ n). The second line contains 2·n distinct integers, representing the indices of the blue nodes. All node indices are between 0 and 4·n - 1 inclusive.

outputFormat

Output one integer representing the maximum number of points (i.e. the maximum matching size) that Daiyousei can achieve.

sample

1
0 1
2

</p>