B. Linear Algebra Test
 
time limit per test

3.0 s

memory limit per test

256 MB

input

standard input

output

standard output

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 jthmatrix.

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/printfinstead 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.

这个题就是用结构体直接比较行列会超时,看数据范围。。。要用map写,猪队友的mapヾ(◍°∇°◍)ノ゙

ma的first是行的值n,ma的second是行n出现的次数,然后mb是列的。嗯,就这样。

还有一个地方就是这个题数据有点大,ans那里要用long long,队友在这里被卡了一手(我不管,反正我队友最厉害(Dog脸))

代码:

 1 //B-用map写,学一手-跑过了
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 #include<algorithm>
6 #include<cmath>
7 #include<map>
8 using namespace std;
9 typedef long long ll;
10 int main(){
11 int t,n;
12 scanf("%d",&t);
13 while(t--){
14 scanf("%d",&n);
15 map<int ,int>ma;
16 map<int ,int>mb;
17 for(int i=0;i<n;i++){
18 int x,y;
19 scanf("%d%d",&x,&y);
20 ma[x]++;
21 mb[y]++;
22 }
23 map<int ,int>::iterator it;
24 ll ans=0;
25 for(it=ma.begin();it!=ma.end();it++){
26 int s1=it->first;
27 int s2=it->second;
28 ans+=(ll)mb[s1]*s2;
29 }
30 printf("%lld\n",ans);
31 }
32 return 0;
33 }

Codeforces Gym101502 B.Linear Algebra Test-STL(map)的更多相关文章

  1. Here’s just a fraction of what you can do with linear algebra

    Here’s just a fraction of what you can do with linear algebra The next time someone wonders what the ...

  2. 2017 JUST Programming Contest 3.0 B. Linear Algebra Test

    B. Linear Algebra Test time limit per test 3.0 s memory limit per test 256 MB input standard input o ...

  3. Linear Algebra lecture1 note

    Professor: Gilbert Strang Text: Introduction to Linear Algebra http://web.mit.edu/18.06   Lecture 1 ...

  4. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  5. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  6. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  7. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  8. 算法库:基础线性代数子程序库(Basic Linear Algebra Subprograms,BLAS)介绍

    调试DeepFlow光流算法,由于作者给出的算法是基于Linux系统的,所以要在Windows上运行,不得不做大量的修改工作.移植到Windows平台,除了一些头文件找不到外,还有一些函数也找不到.这 ...

  9. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

随机推荐

  1. poj-2386 lake counting(搜索题)

    Time limit1000 ms Memory limit65536 kB Due to recent rains, water has pooled in various places in Fa ...

  2. Hadoop4.2HDFS测试报告之三

    第一组:文件存储写过程记录 NameNode:1 DataNode:1 本地存储 scp localpath romotepath 500 2 1 23.67 NameNode:1 DataNode: ...

  3. SPOJ QTREE4 - Query on a tree IV 树分治

    题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...

  4. loj2027 「SHOI2016」黑暗前的幻想乡

    矩阵树定理+模意义下整数高斯消元 #include <algorithm> #include <iostream> #include <cstring> #incl ...

  5. 02-python进阶-文件操作

    新建一个文件 内容是从 0-9的整数 #coding:utf-8 f = open('1.txt','w') #r 只读 w 可写 a追加 for i in range(0,10): f.write( ...

  6. phpcms 后台也名称

    announce 公告 show.html 内容页 comment 评论 show_list.html 内容页评论列表 list.html 评论列表 content 内容模型 category.htm ...

  7. pip install ImportError: cannot import name main

    在Ubuntu上用pip install装ansible时报错 root@user:~# pip install --no-cache-dir ansible -i http://mirrors.al ...

  8. PHPSTORM+Thinkphp3.2模板标签替换Thinkphp5.1公式

    FORMAT: purpose: find: replace 替换<php>标签 <php>(.*)</php> {php}$1{/php} 替换<if &g ...

  9. HDU——1418抱歉(平面欧拉公式)

    抱歉 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  10. POJ-1190 生日蛋糕 NOI99

    深搜+几个剪枝. 貌似搜索顺序也挺重要的...我不知是不是因为这个然后Tle了好久... #include <cstdio> #include <iostream> #incl ...