CF1411C.Peaceful Rooks
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a n×n chessboard. Rows and columns of the board are numbered from 1 to n . Cell (x,y) lies on the intersection of column number x and row number y .
Rook is a chess piece, that can in one turn move any number of cells vertically or horizontally. There are m rooks ( m<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) , where 1≤i≤n .
输入格式
The first line contains the number of test cases t ( 1≤t≤103 ). Description of the t test cases follows.
The first line of each test case contains two integers n and m — size of the chessboard and the number of rooks ( 2≤n≤105 , 1≤m<n ). Each of the next m lines contains two integers xi and yi — positions of rooks, i -th rook is placed in the cell (xi,yi) ( 1≤xi,yi≤n ). It's guaranteed that no two rooks attack each other in the initial placement.
The sum of n over all test cases does not exceed 105 .
输出格式
For each of t 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:
- (2,3)→(2,2)
- (2,1)→(2,3) , (1,2)→(1,1) , (2,3)→(2,2)
- (2,3)→(2,4) , (2,4)→(4,4) , (3,1)→(3,3) , (1,2)→(1,1)