1411C.md (2112B)
1 Source: https://codeforces.com/problemset/problem/1411/C 2 3 C. Peaceful Rooks 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a $n \times 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$. 22 23 Rook is a chess piece, that can in one turn move any number of cells vertically or horizontally. There are $m$ rooks ($m \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. 24 25 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? 26 27 The main diagonal of the chessboard is all the cells $(i, i)$, where $1 \le i \le n$. 28 29 Input 30 31 The first line contains the number of test cases $t$ ($1 \leq t \leq 10^3$). Description of the $t$ test cases follows. 32 33 The first line of each test case contains two integers $n$ and $m$ — size of the chessboard and the number of rooks ($2 \leq n \leq 10^5$, $1 \leq m \lt n$). Each of the next $m$ lines contains two integers $x_i$ and $y_i$ — positions of rooks, $i$-th rook is placed in the cell $(x_i, y_i)$ ($1 \leq x_i, y_i \leq n$). It's guaranteed that no two rooks attack each other in the initial placement. 34 35 The sum of $n$ over all test cases does not exceed $10^5$. 36 37 Output 38 39 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. 40 41 It can be proved that this is always possible. 42 43 Example 44 45 Input 46 47 ``` 48 4 49 3 1 50 2 3 51 3 2 52 2 1 53 1 2 54 5 3 55 2 3 56 3 1 57 1 2 58 5 4 59 4 5 60 5 1 61 2 2 62 3 3 63 ``` 64 65 Output 66 67 ``` 68 1 69 3 70 4 71 2 72 ``` 73 74 Note 75 76 Possible moves for the first three test cases: 77 78 1. $(2, 3) \to (2, 2)$ 79 2. $(2, 1) \to (2, 3)$, $(1, 2) \to (1, 1)$, $(2, 3) \to (2, 2)$ 80 3. $(2, 3) \to (2, 4)$, $(2, 4) \to (4, 4)$, $(3, 1) \to (3, 3)$, $(1, 2) \to (1, 1)$