#P9413. Largest Same-colored Connected Block

    ID: 22565 Type: Default 1000ms 256MiB

Largest Same-colored Connected Block

Largest Same-colored Connected Block

You are given an island grid called Feng Yu consisting of n rows and m columns. Each island is denoted by its coordinates \((i,j)\) for \(1 \le i \le n\) and \(1 \le j \le m\).

The gravity coefficient of the island \((i,j)\) is defined as \[ g_{i,j}=a_i+b_j, \] where a and b are two given arrays of length n and m respectively.

Two islands \((x,y)\) and \((z,w)\) are adjacent if and only if \[ |x-z|+|y-w|=1. \] They are said to be connected if at least one of the following conditions holds:

  • They are adjacent and have equal gravity coefficients, i.e. \(g_{x,y}=g_{z,w}\).
  • There exists another island \((u,v)\) such that \((x,y)\) is connected to \((u,v)\) and \((u,v)\) is connected to \((z,w)\) (the connectivity is transitive).

An unordered set of distinct islands is called a same-colored connected block if every pair of islands in the set is connected (i.e. they all share the same gravity coefficient and are connected by a sequence of adjacent islands having that same value).

Your task is to find the largest same-colored connected block and output two numbers: the size of this block and the number of such blocks that have this maximum size.

inputFormat

The input consists of three lines:

  • The first line contains two integers n and m.
  • The second line contains n integers representing the array a.
  • The third line contains m integers representing the array b.

You can assume that the input is valid.

outputFormat

Output two space‐separated integers: the maximum size of a same-colored connected block and the number of such blocks with that size.

sample

2 2
1 2
3 4
1 4