#P9413. 「NnOI R1-T2」风屿

「NnOI R1-T2」风屿

Background

"Named after the wind, the isles sing together." — Wind Isle.

Problem Description

Wind Isle is an archipelago with nn rows and mm columns. The cell in row ii and column jj is denoted as (i,j)(i,j).

The gravity system of Wind Isle is very strange. The gravity coefficient of (i,j)(i,j) is gi,j=ai+bjg_{i,j}=a_i+b_j. Here, aa and bb are two known arrays of lengths nn and mm.

We say islands (x,y)(x,y) and (z,w)(z,w) are adjacent if and only if xz+yw=1|x-z|+|y-w|=1. We say (x,y)(x,y) and (z,w)(z,w) are connected if and only if at least one of the following holds:

  • (x,y)(x,y) and (z,w)(z,w) are adjacent, and gx,y=gz,wg_{x,y}=g_{z,w}.

  • There exists another island (u,v)(u,v) such that (x,y)(x,y) is connected to (u,v)(u,v) and (u,v)(u,v) is connected to (z,w)(z,w). That is, the connected relation is transitive.

We define an unordered set of distinct islands {(xi,yi)}\{(x_i,y_i)\} to be a same-color connected component if and only if every pair of islands in the set is connected.

Find the largest same-color connected component, and output its size and the number of such components.

Input Format

This problem has multiple test cases. The first line contains a positive integer TT, representing the number of testdata groups in this test.

For each test case:

The first line contains nn and mm, representing the number of rows and columns of Wind Isle.

The next line contains nn integers, representing array aa.

The next line contains mm integers, representing array bb.

Output Format

Output TT lines. Each line contains the answer for that test case (the maximum component size and the count).

3
3 4
1 2 2
1 2 3 4
4 5
1 2 2 3
2 3 3 3 4
6 7
1 1 2 2 3 4
1 2 2 2 3 3 3
2 4
6 1
6 4

Hint

Sample Explanation

For sample 11:

For the first test case, the gravity coefficients are as follows:

2 3 4 5
3 4 5 6
3 4 5 6
2 3 4 5
* # ? .
* # ? .

The cells marked by symbols form the largest same-color connected components. The size is 22, and there are 44 such components.

Constraints

For 20%20\% of the data, n,m103n,m \le 10^3.

For another 20%20\% of the data, all bib_i are equal.

For another 20%20\% of the data, the answer to the second question is guaranteed to be 11.

For another 20%20\% of the data, T=1T=1. The test points represented by these four subtasks do not include each other.

For 100%100\% of the data, 1T51 \le T \le 5, 1n,m1051 \le n,m \le 10^5, 1ai,bi1091 \le a_i,b_i \le 10^9.

Source

Item People
idea Kevin0501
std
data EstasTonne
check
solution Kevin0501

Translated by ChatGPT 5