B - Linear Algebra Test Gym - 101502B

Dr. Wail is preparing for today's test in linear algebra course. The test's subject is Matrices Multiplication.

Dr. Wail has n matrices, such that the size of the ith matrix is (ai × bi), where ai is the number of rows in the ith matrix, and bi is the number of columns in the ith matrix.

Dr. Wail wants to count how many pairs of indices i and j exist, such that he can multiply the ith matrix with the jth matrix.

Dr. Wail can multiply the ith matrix with the jth matrix, if the number of columns in the ith matrix is equal to the number of rows in the jth matrix.

Input

The first line contains an integer T (1 ≤ T ≤ 100), where T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105), where n is the number of matrices Dr. Wail has.

Then n lines follow, each line contains two integers ai and bi (1 ≤ ai, bi ≤ 109) (ai ≠ bi), where ai is the number of rows in the ith matrix, and bi is the number of columns in the ith matrix.

Output

For each test case, print a single line containing how many pairs of indices i and j exist, such that Dr. Wail can multiply the ith matrix with the jth matrix.

Example

Input

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

Output

5

Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

In the first test case, Dr. Wail can multiply the 1st matrix (2 × 3) with the 4th matrix (3 × 5), the 2nd matrix (2 × 3) with the 4th matrix (3 × 5), the 3rd matrix (4 × 2) with the 1st and second matrices (2 × 3), and the 5th matrix (9 × 4) with the 3rd matrix (4 × 2). So, the answer is 5.

第一个代码为超时代码,此题要用到二分查找

第二个代码为AC代码

#include <iostream>

using namespace std;

int main()
{
    int a,i,b,j,t;
    int n[100010],m[100010];
    cin >> a;
    while(a--)
    {
        t=0;
        cin >> b;
        for(i=1; i<=b; i++)
        {
            cin >> n[i] >> m[i];
        }
        for(i=1; i<=b; i++)
        {
            for(j=i+1; j<=b; j++)
            {
                if(n[i]==m[j])
                {
                    t++;
                }
                if(m[i]==n[j])
                {
                    t++;
                }
            }
        }
        cout << t << endl;;
    }
    return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int f(int n[],int a,int b,int c)
{
    int mid;
    mid=(a+b)/2;
    while(a<=b)
    {
        if(n[mid]==c)
        {
            return mid+1;
        }
        else if(n[mid]>c)
        {
            return f(n,a,mid-1,c);
        }
        else
        {
            return f(n,mid+1,b,c);
        }
    }
    return -1;
}

int main()
{
    long long int sum,k;
    int t,a,i,b,j,n[100010],m[100010];
    scanf("%d",&a);
    while(a--)
    {
        k=0;
        sum=0;
        cin >> b;
        for(i=0; i<b; i++)
        {
            scanf("%d%d",&n[i],&m[i]);
        }
        sort(n,n+b);
        sort(m,m+b);
        for(i=0; i<b; i++)
        {
            if(i==0||n[i]!=n[i-1])
            {
                k=0;
                t=f(m,0,b-1,n[i]);
                if(t!=-1)
                {
                    for(j=t; j<b; j++)
                    {
                        if(n[i]==m[j])
                        {
                            k++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    for(j=t-1; j>=0; j--)
                    {
                        if(n[i]==m[j])
                        {
                            k++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    sum=sum+k;
                }
            }
            else
            {
                sum=sum+k;
            }
        }
        cout << sum << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZZ --瑞 hopeACMer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值