#P4380. [USACO18OPEN] Multiplayer Moo S

    ID: 3316 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2018USACO并查集枚举,暴力随机贪心,随机化

[USACO18OPEN] Multiplayer Moo S

Description

The cows have invented a new game, and surprisingly, they gave it the least creative name: "Moo". The Moo game is played on a board consisting of N×NN \times N square cells. A cow can claim a cell by shouting "Moo!" and then writing her numeric ID in that cell.

At the end of the game, each cell contains a number. At that point, if a cow has created a region of connected cells and that region is at least as large as every other region, then that cow wins. A "region" is defined as a set of cells with the same numeric ID, where each cell is directly adjacent (up, down, left, or right) to another cell in the same region (diagonals do not count).

Since playing solo can be a bit boring, the cows are also interested in playing in two-cow teams. Two cows on the same team can form a region, but now the cells in the region may belong to either cow on the team.

Given the final state of the game board, please help the cows compute:

  1. The number of cells in the largest region owned by any single cow.
  2. The number of cells in the largest region owned by any team of two cows.

Note that a region owned by two cows must contain IDs from both cows on the team; it cannot contain only one cow’s ID.

Input Format

The first line contains NN (1N2501 \leq N \leq 250). Each of the next NN lines contains NN integers (each in the range 01060 \ldots 10^6), describing the final state of the board. The board contains at least two distinct numbers.

Output Format

Output two lines: the first line is the size of the largest region owned by any single cow, and the second line is the size of the largest region owned by any two-cow team.

4
2 3 9 3
4 9 9 1
9 9 1 7
2 1 1 9
5
10

Hint

In this example, the largest single-cow region consists of five 99s. If cows with IDs 11 and 99 form a team, they can form a region of size 1010.

Problem by: Brian Dean.

Translated by ChatGPT 5