LCC '24 Contest 3 S5 - A Game of Risk

View as PDF

Submit solution

Points: 12
Time limit: 3.0s
PyPy 3 4.0s
Memory limit: 977M

Author:
Problem types

From the spaceship, you look down upon Earth. You quickly notice that the sight you see can be described as a \(N × N\) grid, where each cell represents a plot of land. Unfortunately, you've spent so much time in the spaceship that it's now World War III!

Each cell in the grid is waving a flag with a number, a_i on it, which indicates different countries. All the countries are currently hostile with one another. Countries that own pieces of land will have some "regions". However, countries can have multiple "regions" that are not necessarily connected to each other.

A region can be defined as a group of connected cells that share an edge and are part of the same country.

In war, countries can form an alliance. If two countries form an alliance, the cells in their region can belong to either of the two countries. Determine the size of the largest region if any two countries were to form an alliance.

Constraints

Subtask 2 [20%]

2 \le N \le 10

1 \le a_i \le 10

Subtask 1 [80%]

1 \le N \le 250

1 \le a_i \le 10^9

Input Specification

The first line will contain the integer N, where \(N × N\) is the size of the grid.

The next N lines will contain N integers, a_i, representing the country the cell belongs to.

Output Specification

The first line of output will represent the number of cells the largest region can have if any two countries were to form an alliance.

Sample Input 1

4
2 3 9 3
4 9 9 1
9 9 1 7
2 1 1 9

Sample Output 1

10

Sample Explanation 1

If country 1 and 9 form an alliance, then the maximum amount of cells in a connected region is 10. Notice that the 1 in the top left corner would form a region by itself, having a size of 1. It is not part of the purple region because no edge is shared between the two regions.


Comments

There are no comments at the moment.