B. Seating of Students

Codeforces
IDCF906B
Time2000ms
Memory256MB
Difficulty
brute forceconstructive algorithmsmath
English · Original
Chinese · Translation
Formal · Original
Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating." The class can be represented as a matrix with _n_ rows and _m_ columns with a student in each cell. Two students are neighbors if cells in which they sit have a common side. Let's enumerate students from 1 to _n_·_m_ in order of rows. So a student who initially sits in the cell in row _i_ and column _j_ has a number (_i_ - 1)·_m_ + _j_. You have to find a matrix with _n_ rows and _m_ columns in which all numbers from 1 to _n_·_m_ appear exactly once and adjacent numbers in the original matrix are not adjacent in it, or determine that there is no such matrix. ## Input The only line contains two integers _n_ and _m_ (1 ≤ _n_, _m_ ≤ 105; _n_·_m_ ≤ 105) — the number of rows and the number of columns in the required matrix. ## Output If there is no such matrix, output "_NO_" (without quotes). Otherwise in the first line output "_YES_" (without quotes), and in the next _n_ lines output _m_ integers which form the required matrix. [samples] ## Note In the first test case the matrix initially looks like this: 1 2 3 4 5 6 7 8 It's easy to see that there are no two students that are adjacent in both matrices. In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors.
学生进入教室参加考试,并以某种方式就座。老师想:"他们可能是为了互相抄袭而这样坐的。我需要重新安排他们的座位,使得原来相邻的学生在新座位中不再相邻。" 班级可以表示为一个包含 #cf_span[n] 行和 #cf_span[m] 列的矩阵,每个单元格中有一名学生。如果两名学生所在的单元格有公共边,则称他们为相邻。 我们将学生从 #cf_span[1] 到 #cf_span[n·m] 按行顺序编号。因此,最初坐在第 #cf_span[i] 行、第 #cf_span[j] 列的学生编号为 #cf_span[(i - 1)·m + j]。你需要找到一个包含 #cf_span[n] 行和 #cf_span[m] 列的矩阵,其中包含从 #cf_span[1] 到 #cf_span[n·m] 的所有数字且每个数字恰好出现一次,并且在原矩阵中相邻的数字在新矩阵中不再相邻;若不存在这样的矩阵,则指出这一点。 输入仅包含一行,两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n, m ≤ 105];#cf_span[n·m ≤ 105])——表示所需矩阵的行数和列数。 如果不存在这样的矩阵,请输出 "_NO_"(不含引号)。 否则,第一行输出 "_YES_"(不含引号),随后的 #cf_span[n] 行每行输出 #cf_span[m] 个整数,构成所需的矩阵。 在第一个测试用例中,初始矩阵如下: 可以很容易看出,没有任何两名学生在两个矩阵中都是相邻的。 在第二个测试用例中,只有两种可能的座位安排,而在两种安排中,编号为 1 和 2 的学生都是相邻的。 ## Input 输入仅包含一行,两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n, m ≤ 105];#cf_span[n·m ≤ 105])——表示所需矩阵的行数和列数。 ## Output 如果不存在这样的矩阵,请输出 "_NO_"(不含引号)。否则,第一行输出 "_YES_"(不含引号),随后的 #cf_span[n] 行每行输出 #cf_span[m] 个整数,构成所需的矩阵。 [samples] ## Note 在第一个测试用例中,初始矩阵如下: 1 2 3 4 5 6 7 8 可以很容易看出,没有任何两名学生在两个矩阵中都是相邻的。 在第二个测试用例中,只有两种可能的座位安排,而在两种安排中,编号为 1 和 2 的学生都是相邻的。
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ with $ 1 \leq n, m \leq 10^5 $ and $ n \cdot m \leq 10^5 $. Let $ G = (V, E) $ be the grid graph with $ |V| = n \cdot m $, where each vertex corresponds to a cell $ (i,j) $ for $ 1 \leq i \leq n $, $ 1 \leq j \leq m $, and edges connect horizontally/vertically adjacent cells. Label vertices by $ \ell(i,j) = (i-1)m + j $. Let $ H $ be a relabeling of $ V $ via a bijection $ f: V \to \{1, 2, \dots, nm\} $, such that for all $ u,v \in V $, if $ (u,v) \in E $, then $ |f(u) - f(v)| \neq 1 $. **Constraints** - $ n \cdot m \leq 10^5 $ **Objective** Determine whether there exists a bijection $ f $ such that no two originally adjacent cells (sharing a side) are assigned consecutive integers. If such $ f $ exists, output the $ n \times m $ matrix of $ f(i,j) $; otherwise, output "NO".
Samples
Input #1
2 4
Output #1
YES
5 4 7 2 
3 6 1 8
Input #2
2 1
Output #2
NO
API Response (JSON)
{
  "problem": {
    "name": "B. Seating of Students",
    "description": {
      "content": "Students went into a class to write a test and sat in some way. The teacher thought: \"Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF906B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Students went into a class to write a test and sat in some way. The teacher thought: \"Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "学生进入教室参加考试,并以某种方式就座。老师想:\"他们可能是为了互相抄袭而这样坐的。我需要重新安排他们的座位,使得原来相邻的学生在新座位中不再相邻。\"\n\n班级可以表示为一个包含 #cf_span[n] 行和 #cf_span[m] 列的矩阵,每个单元格中有一名学生。如果两名学生所在的单元格有公共边,则称他们为相邻。\n\n我们将学生从 #cf_span[1] 到 #cf_span[n·m] 按行顺序编...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ with $ 1 \\leq n, m \\leq 10^5 $ and $ n \\cdot m \\leq 10^5 $.  \nLet $ G = (V, E) $ be the grid graph with $ |V| = n \\cdot m $, where each vertex correspon...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments