Given an N x N grid of cow IDs, find the largest connected region of one ID and the largest region formed by two IDs together.
Hard8DFSGraphUnion-findImplementationNo attempts yetTime limit2sMemory limit512 MBThe cows invented a new game and gave it the least creative name they could think of: "Moo".
Moo is played on an N×N grid of square cells. A cow claims a cell by yelling "moo!" and writing her numeric ID in that cell.
When the game ends, every cell holds one number. A cow wins if the region she built is no smaller than every other region. A region is a group of cells that all hold the same ID, where each cell of the group touches another cell of the group above, below, to the left, or to the right. Diagonal contact does not count.
Playing alone is dull, so the cows also pair up into teams of two. A team builds a region the same way, except that a cell of the region may hold the ID of either cow on the team. A region counts for a team only when both IDs appear in it. A region holding just one of the two IDs does not count.
You are given the board at the end of the game. Compute the number of cells in the largest region owned by a single cow, and the number of cells in the largest region a team of two cows can claim.
The first line contains N (1≤N≤250). Each of the next N lines contains N integers describing the board at the end of the game. Each integer is between 0 and 106. At least two distinct IDs appear on the board.
Print the size of the largest region claimed by a single cow on the first line, and the size of the largest region a team of two cows can claim on the second line.
In the first example the largest region built by a single cow is the five cells holding 9. The cows with IDs 1 and 9 can team up for a region of size 10.