第 77 场 AtCoder Grand Contest
B
最长递增游走
Long Increasing Walk
AtCoder
⌀ 2794
原题
← AC →

最长递增游走AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given positive integers $N$ and $K$.

给定正整数 $N$ 和 $K$。AI (CI翻译)

There is a complete bipartite graph $G$ with $N$ vertices $l_1, l_2, \ldots, l_N$ on the left and $N$ vertices $r_1, r_2, \ldots, r_N$ on the right. It has $N^2$ edges; for each $(i, j)\ (1\leq i\leq N, 1\leq j\leq N)$, there is one undirected edge between vertices $l_i$ and $r_j$.

有一个完全二分图 $G$,左侧有 $N$ 个顶点 $l_1, l_2, \ldots, l_N$,右侧有 $N$ 个顶点 $r_1, r_2, \ldots, r_N$。该图有 $N^2$ 条边;对于每个 $(i, j)\ (1\leq i\leq N, 1\leq j\leq N)$,顶点 $l_i$ 与 $r_j$ 之间有一条无向边。AI (CI翻译)

Determine whether there exists a way to write an integer between $1$ and $N^2$, inclusive, on each edge of $G$ (where the $N^2$ integers written are all distinct) such that the answer to the following problem is $K$, and if such a way exists, find one.

请判断是否存在一种方案,在 $G$ 的每条边上分别写入一个 $1$ 到 $N^2$(包含两端)的整数,并且写入的 $N^2$ 个整数互不相同,使得下面问题的答案为 $K$;如果存在,请构造出一种方案。AI (CI翻译)

Among all walks on graph $G$, we call a walk good if the sequence of numbers written on the edges traversed, in the order they are traversed, is strictly increasing.

Find the maximum number of edges contained in a good walk.

在图 $G$ 的所有游走中,如果沿游走经过的边上的数字按照经过顺序严格递增,则称该游走是优秀的。

求优秀游走所包含的边数的最大值。

AI (CI翻译)

$T$ test cases are given; solve each of them.

共有 $T$ 组测试数据,请分别求解。AI (CI翻译)

What is a walk? A walk on graph $G$ is a sequence of $k$ vertices and $k-1$ edges alternately arranged, $(v_1 ​ ,e_1 ​ ,v_2 ​ ,\ldots,v_{k−1} ​ ,e_{k−1} ​ ,v_k)$ (where $k$ is a positive integer), such that edge $e_i$ connects vertices $v_i$ and $v_{i+1}$.

什么是游走?图 $G$ 上的一个游走是由 $k$ 个顶点和 $k-1$ 条边交替组成的序列 $(v_1 ​ ,e_1 ​ ,v_2 ​ ,\ldots,v_{k−1} ​ ,e_{k−1} ​ ,v_k)$(其中 $k$ 为正整数),并且边 $e_i$ 连接顶点 $v_i$ 和 $v_{i+1}$。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1\leq T\leq 3000$
  • $1\leq N\leq 700$
  • $1\leq K\leq N^2$
  • The sum of $N^2$ in each input is at most $5\times 10^5$.
  • All input values are integers.
  • $1\leq T\leq 3000$
  • $1\leq N\leq 700$
  • $1\leq K\leq N^2$
  • 所有输入中 $N^2$ 的总和不超过 $5\times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入按以下格式给出:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each test case is given in the following format:

每组测试数据按以下格式给出:AI (CI翻译)

N K

N K
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answers for $\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T$ in this order in the following format.

按照 $\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T$ 的顺序,按以下格式输出答案。AI (CI翻译)

If no valid way of writing exists, output No.

如果不存在有效的填写方案,输出 No。AI (CI翻译)

If a valid way exists, letting $A_{i,j}$ be the integer written on the edge connecting vertices $l_i$ and $r_j$,

如果存在有效方案,设连接顶点 $l_i$ 和 $r_j$ 的边上写入的整数为 $A_{i,j}$,则输出:AI (CI翻译)

Yes
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}

Yes
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
AI (CI翻译)

output as above. $(A_{1,1},\ldots,A_{1,N},A_{2,1},\ldots,A_{2,N},\ldots,A_{N,1},\ldots,A_{N,N})$ must be a permutation of $(1,2,\ldots,N^2)$.

其中 $(A_{1,1},\ldots,A_{1,N},A_{2,1},\ldots,A_{2,N},\ldots,A_{N,1},\ldots,A_{N,N})$ 必须是 $(1,2,\ldots,N^2)$ 的一个排列。AI (CI翻译)

If multiple solutions exist, any of them will be accepted.

如果存在多个解,输出其中任意一个即可。AI (CI翻译)

样例

Input #1
3
2 1
2 3
3 5
Output #1
No
Yes
1 2
3 4
Yes
2 9 7
4 5 6
1 8 3
For the first test case, no valid way of writing exists. For the second test case, a good walk containing three edges is $r_1 \xrightarrow{1} l_1 \xrightarrow{2}r_2\xrightarrow{4}l_2$. No good walk contains four or more edges, so this output satisfies the condition. For the third test case, a good walk containing five edges is $l_1 \xrightarrow{2} r_1 \xrightarrow{4}l_2\xrightarrow{6}r_3 \xrightarrow{7} l_1 \xrightarrow{9} r_2$. No good walk contains six or more edges, so this output satisfies the condition. ![image](https://img.atcoder.jp/agc077/483f3ac7fef3640067cde0b569f0be80.png)
题目信息
题号
agc077_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。