并查集-判断是否成环-棋子的移动

You are given a n×n chessboard. Rows and columns of the board are numbered from1 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 mm 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.

Input

The first line contains the number of test cases tt (1≤t≤103). Description of the tttest 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 mmlines contains two integers xi and yi — positions of rooks, ii-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 nn over all test cases does not exceed 105.

Output

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.

Example

Input

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

Output

1
3
4
2

Note

Possible moves for the first three test cases:

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

题解:不能有两个点在同一行或者同一列,移动的时候也不可以,看里边的有没有构成这样的环,比如说(1,2),(2,3),(3,1);这种类型的。构成的话其中一个点就不得不走两步,判断是否构成这样的环可以用并查集来写。

代码:

#include<stdio.h>
#include<string.h>
int n,m,x,y,f[100010];
int gef(int v)
{
    return v==f[v]?v:f[v]=gef(f[v]);
}
int marge(int u,int v)
{
    int g1,g2;
    g1=gef(u);
    g2=gef(v);
    if(g1!=g2)
    {
        f[g2]=g1;
        return 0;
    }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.

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.

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.

Example
Input
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
Output
1
3
4
2
Note
Possible moves for the first three test cases:

(2,3)→(2,2)(2,3)→(2,2)
(2,1)→(2,3)(2,1)→(2,3), (1,2)→(1,1)(1,2)→(1,1), (2,3)→(2,2)(2,3)→(2,2)
(2,3)→(2,4)(2,3)→(2,4), (2,4)→(4,4)(2,4)→(4,4), 
    else
    return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int ans=0,i;
        for(i=1;i<=100010;i++)
            f[i]=i;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            if(x==y)
            continue;
            if(marge(x,y))
            {
                ans++;
            }
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值