A. Peaceful Rooks

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


1.Problem

You are given a n×nn×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<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≤i≤n.

2.Input

The first line contains the number of test cases tt (1≤t≤1031≤t≤103). 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≤n≤105, 1≤m<n1≤m<n). Each of the next mm lines contains two integers xixi and yiyi — positions of rooks, ii-th rook is placed in the cell (xi,yi)(xi,yi) (1≤xi,yi≤n1≤xi,yi≤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 105105.

3.Output

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.

4.Examples

4.1input

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

4.2output

1
3
4
2

5.Code

#include<bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <typename T> bool chkmax(T &x, T y) { return x < y ? (x = y, true) : false; }
template <typename T> bool chkmin(T &x, T y) { return x > y ? (x = y, true) : false; }

int readint() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

const int MAXN = 100005;
int a[MAXN], vis[MAXN];

void solve() {
    int n = readint();
    int m = readint();
    
    fill(a, a + n + 1, 0);
    fill(vis, vis + n + 1, 0);

    int ans = 0, x, y;

    for(int i = 1; i <= m; i++) {
        x = readint();
        y = readint();
        a[x] = y;
        if(x != y) ans++;
    }

    for(int i = 1; i <= n; i++) {
        if(vis[i] || a[i] == i) continue;
        vis[i] = 1;
        bool fl = 1;
        for(int j = a[i]; j != i; j = a[j]) {
            if(!j) {
                fl = 0;
                break;
            }
            vis[j] = 1;
        }
        if(fl) ans++;
    }

    printf("%d\n", ans);
}

int main() {
    int T = readint();
    while(T--) {
        solve();
    }
    return 0;
}

6.Conclusion

        上面的代码是一个C++程序,其主要功能是读取输入数据,处理多组测试用例,然后输出结果。以下是代码的主要组成部分和功能:

1.头文件和宏定义:

        引入了 &lt;bits/stdc++.h&gt; 头文件,该头文件包含了C++标准库的所有头文件,通常在竞赛和编程中使用。
        定义了一些宏,如 pb、mp、fi、se,用于简化代码。

2.命名空间和类型别名:

        使用了 using namespace std;,表示使用标准命名空间。
        定义了一些类型别名,如 ll 表示 long long,pii 表示 pair&lt;int, int&gt;,等等。

3.模板函数 chkmax 和 chkmin:

        这两个函数用于更新变量的最大值和最小值。

4.输入函数 readint:

        读取整数输入,并处理负号。

5.全局常量和数组定义:

        定义了一个常量 MAXN,表示数组的最大大小。
        定义了两个整数数组 a 和 vis,分别用于存储输入数据和标记访问情况。

6.解题函数 solve:

        读取测试用例的输入数据,包括整数 n 和 m。
        使用数组 a 记录了一些映射关系。
        统计满足条件的元素对,并输出结果。

7.主函数 main:

        读取测试用例的数量 T。
        调用 solve 函数解决每个测试用例。

8.程序结尾:

        返回 0,表示程序正常运行结束。

        总体而言,这段代码是一个处理多组测试用例的程序,每个测试用例包括两个整数 n 和 m,通过一些数组操作和条件判断,统计满足条件的元素对的数量,并输出结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值