CF1411C.Peaceful Rooks

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a n×nn \times n chessboard. Rows and columns of the board are numbered from 11 to nn. Cell (x,y)(x, y) lies on the intersection of column number xx and row number yy.

Rook is a chess piece, that can in one turn move any number of cells vertically or horizontally. There are mm rooks (m<nm \lt n) placed on the chessboard in such a way that no pair of rooks attack each other. I.e. there are no pair of rooks that share a row or a column.

In one turn you can move one of the rooks any number of cells vertically or horizontally. Additionally, it shouldn't be attacked by any other rook after movement. What is the minimum number of moves required to place all the rooks on the main diagonal?

The main diagonal of the chessboard is all the cells (i,i)(i, i), where 1≤i≤n1 \le i \le n.

给你一个 n×nn \times n 的棋盘。棋盘的行与列均从 11 编号至 nn。单元格 (x,y)(x, y) 位于第 xx 列与第 yy 行的交点处。

车(Rook)是一种国际象棋棋子,它在一回合中可沿垂直或水平方向移动任意格数。现有 mm 个车(m<nm \lt n)被放置在棋盘上,且彼此互不攻击,即任意两个车均不在同一行或同一列上。

在一回合中,你可以将其中一个车沿垂直或水平方向移动任意格数;移动后,该车不能受到其余任何车的攻击。问:将所有车都放置到主对角线上的最少移动次数是多少?

棋盘的主对角线指所有形如 (i,i)(i, i) 的单元格,其中 1≤i≤n1 \le i \le n。

输入格式

The first line contains the number of test cases tt (1≤t≤1031 \leq t \leq 10^3). Description of the tt test cases follows.

The first line of each test case contains two integers nn and mm — size of the chessboard and the number of rooks (2≤n≤1052 \leq n \leq 10^5, 1≤m<n1 \leq m \lt n). Each of the next mm lines contains two integers xix_i and yiy_i — positions of rooks, ii-th rook is placed in the cell (xi,yi)(x_i, y_i) (1≤xi,yi≤n1 \leq x_i, y_i \leq n). It's guaranteed that no two rooks attack each other in the initial placement.

The sum of nn over all test cases does not exceed 10510^5.

第一行包含测试用例的数量 tt(1≤t≤1031 \leq t \leq 10^3)。接下来描述 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn 和 mm —— 分别表示棋盘的大小和车的数量(2≤n≤1052 \leq n \leq 10^5,1≤m<n1 \leq m \lt n)。接下来的 mm 行每行包含两个整数 xix_i 和 yiy_i —— 表示第 ii 辆车的位置,即该车位于格子 (xi,yi)(x_i, y_i) 上(1≤xi,yi≤n1 \leq x_i, y_i \leq n)。保证初始放置中任意两辆车间互不攻击。

所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each of tt test cases print a single integer — the minimum number of moves required to place all the rooks on the main diagonal.

It can be proved that this is always possible.

对于每个测试用例,输出一个整数——将所有车移动到主对角线所需的最少步数。

可以证明,这总是可行的。

输入输出样例

  • 输入#1

    4
    3 1
    2 3
    3 2
    2 1
    1 2
    5 3
    2 3
    3 1
    1 2
    5 4
    4 5
    5 1
    2 2
    3 3

    输出#1

    1
    3
    4
    2

说明/提示

Possible moves for the first three test cases:

  1. (2,3)→(2,2)(2, 3) \to (2, 2)
  2. (2,1)→(2,3)(2, 1) \to (2, 3), (1,2)→(1,1)(1, 2) \to (1, 1), (2,3)→(2,2)(2, 3) \to (2, 2)
  3. (2,3)→(2,4)(2, 3) \to (2, 4), (2,4)→(4,4)(2, 4) \to (4, 4), (3,1)→(3,3)(3, 1) \to (3, 3), (1,2)→(1,1)(1, 2) \to (1, 1)

前三组测试用例的可能移动步骤:

  1. (2,3)→(2,2)(2, 3) \to (2, 2)
  2. (2,1)→(2,3)(2, 1) \to (2, 3), (1,2)→(1,1)(1, 2) \to (1, 1), (2,3)→(2,2)(2, 3) \to (2, 2)
  3. (2,3)→(2,4)(2, 3) \to (2, 4), (2,4)→(4,4)(2, 4) \to (4, 4), (3,1)→(3,3)(3, 1) \to (3, 3), (1,2)→(1,1)(1, 2) \to (1, 1)

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

首页