CF2245H.Connect Connect See

NOI/NOI+/CTSC

通过率:0%

时间限制:8.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rectangular grid aa consisting of nn rows and mm columns. The jj-th cell in the ii-th row is denoted by (i,j)(i,j). There is a non-negative integer written in every cell. The integer written in cell (i,j)(i,j) is denoted by ai,ja_{i,j}.

A path pp of length kk on aa is defined as a sequence of cells p0,p1,…,pkp_0,p_1,\ldots,p_k such that pip_i and pi+1p_{i+1} share an edge for every 0≤i<k0 \le i \lt k, and all pip_i are distinct. The number of turns of a simple path pp of length kk, denoted by t(p)t(p), is defined as the number of indices 1≤i<k1 \le i \lt k that satisfy the following condition:

  • Let pj=(xj,yj)p_j=(x_j,y_j) for j∈i−1,i,i+1j \in {i-1,i,i+1}. Then, both xi−1≠xi+1x_{i-1} \ne x_{i+1} and yi−1≠yi+1y_{i-1} \ne y_{i+1} hold.

A path p=[(x0,y0),(x1,y1),…,(xk,yk)]p=[(x_0,y_0),(x_1,y_1),\ldots,(x_k,y_k)] is considered valid if and only if all of the following conditions hold:

  • k≥1k \ge 1.
  • t(p)≤2t(p) \le 2.
  • ax0,y0=axk,yka_{x_0,y_0}=a_{x_k,y_k}, ax0,y0>0a_{x_0,y_0} \gt 0, and axi,yi=0a_{x_i,y_i}=0 for every 1≤i<k1 \le i \lt k.

An unordered pair of cells (u1,v1)(u_1,v_1) and (u2,v2)(u_2,v_2) is considered connectable if and only if there exists a valid path pp of length kk such that p0=(u1,v1)p_0=(u_1,v_1) and pk=(u2,v2)p_k=(u_2,v_2).

Find the number of connectable pairs in aa.

给你一个由 nn 行和 mm 列组成的矩形网格 aa。第 ii 行第 jj 列的格子记为 (i,j)(i,j)。每个格子中写有一个非负整数。格子 (i,j)(i,j) 中的整数记为 ai,ja_{i,j}。

网格 aa 上一条长度为 kk 的路径 pp 定义为一个格子序列 p0,p1,…,pkp_0,p_1,\ldots,p_k,满足:对每个 0≤i<k0 \le i < k,pip_i 与 pi+1p_{i+1} 共享一条边(即相邻),且所有 pip_i 互不相同。一条长度为 kk 的简单路径 pp 的转向次数(记为 t(p)t(p))定义为满足以下条件的下标 ii(其中 1≤i<k1 \le i < k)的个数:

  • 设 pj=(xj,yj)p_j=(x_j,y_j),其中 j∈{i−1,i,i+1}j \in \{i-1,i,i+1\},则同时满足 xi−1≠xi+1x_{i-1} \ne x_{i+1} 且 yi−1≠yi+1y_{i-1} \ne y_{i+1}。

路径 p=[(x0,y0),(x1,y1),…,(xk,yk)]p=[(x_0,y_0),(x_1,y_1),\ldots,(x_k,y_k)] 被称为合法路径,当且仅当满足以下全部条件:

  • k≥1k \ge 1。
  • t(p)≤2t(p) \le 2。
  • ax0,y0=axk,yka_{x_0,y_0}=a_{x_k,y_k},ax0,y0>0a_{x_0,y_0} > 0,且对每个 1≤i<k1 \le i < k,均有 axi,yi=0a_{x_i,y_i}=0。

无序格子对 ((u1,v1),(u2,v2))((u_1,v_1),(u_2,v_2)) 被称为可连接的,当且仅当存在一条合法路径 pp(长度为 kk),使得 p0=(u1,v1)p_0=(u_1,v_1) 且 pk=(u2,v2)p_k=(u_2,v_2)。

求网格 aa 中可连接的格子对的总数。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤1001 \le n \le 100, 1≤n⋅m≤2⋅1061 \le n \cdot m \le 2 \cdot 10^6), representing the dimensions of aa.

The ii-th of the next nn lines contains mm integers ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2},\ldots,a_{i,m} (0≤ai,j≤n⋅m0 \le a_{i,j} \le n \cdot m), representing the ii-th row of aa.

It is guaranteed that the sum of n⋅mn \cdot m over all test cases does not exceed 2⋅1062 \cdot 10^6.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤1001 \le n \le 100,1≤n⋅m≤2⋅1061 \le n \cdot m \le 2 \cdot 10^6),表示矩阵 aa 的维度。

接下来的 nn 行中,第 ii 行包含 mm 个整数 ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2},\ldots,a_{i,m}(0≤ai,j≤n⋅m0 \le a_{i,j} \le n \cdot m),表示矩阵 aa 的第 ii 行。

保证所有测试用例中 n⋅mn \cdot m 的总和不超过 2⋅1062 \cdot 10^6。

输出格式

For each test case, output an integer representing the number of connectable pairs in aa.

对于每个测试用例,输出一个整数,表示数组 aa 中可连接的对数。

输入输出样例

  • 输入#1

    7
    1 1
    1
    1 1
    0
    2 2
    3 3
    0 3
    2 2
    2 0
    0 2
    3 3
    0 0 0
    1 2 1
    0 0 0
    5 5
    1 0 1 0 1
    0 0 0 0 0
    1 0 1 0 1
    0 0 0 0 0
    1 0 1 0 1
    4 4
    0 0 0 0
    1 2 1 2
    1 0 0 1
    0 0 2 0

    输出#1

    0
    0
    3
    1
    1
    34
    7

说明/提示

In the first and second test cases, there are no connectable pairs in aa.

In the third test case, the connectable pairs are:

  • (1,1),(1,2)(1,1),(1,2).
  • (1,2),(2,2)(1,2),(2,2).
  • (1,1),(2,2)(1,1),(2,2).

In the sixth test case, there are 3434 connectable pairs. One of them is (1,1),(3,3)(1,1),(3,3). Note that (1,1),(5,5)(1,1),(5,5) is not a connectable pair, as any path that begins at (1,1)(1,1) and ends at (5,5)(5,5) has more than 22 turns.

在第一和第二个测试用例中,数组 aa 中不存在可连接的点对。

在第三个测试用例中,可连接的点对有:

  • (1,1),(1,2)(1,1),(1,2)。
  • (1,2),(2,2)(1,2),(2,2)。
  • (1,1),(2,2)(1,1),(2,2)。

在第六个测试用例中,共有 3434 个可连接的点对。其中之一是 (1,1),(3,3)(1,1),(3,3)。注意,(1,1),(5,5)(1,1),(5,5) 不是一个可连接的点对,因为任何从 (1,1)(1,1) 出发、终止于 (5,5)(5,5) 的路径的转弯次数均超过 22 次。

输入解题思路,AI测评打分。不知道怎么写?

首页