Description
To ecient calculate the multiplication of a sparse matrix is very useful in industrial led. Let's consider
this problem:
A is an N*N matrix which only contains 0 or 1. And we want to know the result of A*(AT) .
Formally, we de ne B = A*(AT) , Aij is equal to 1 or 0, and we know the number of 1 in matrix A is M
and your task is to calculate B.
Input Description
The input contains several test cases. The first line of input contains a integer C indicating the number
of the cases.
For each test case, the first line contains two integer N and M.
and each of next M lines contains two integer X and Y , which means Axy is 1.
N <=100000, M <=1000, C <=10
of the cases.
For each test case, the first line contains two integer N and M.
and each of next M lines contains two integer X and Y , which means Axy is 1.
N <=100000, M <=1000, C <=10
Output Description
For each test case, it should have a integer W indicating how many element in Matrix B isn´t zero in one
line.
line.
Sample Input
2 5 3 1 0 2 1 3 3 3 3 0 0 1 0 2 0
Sample Output
3 9
B=A*(AT).则Bij=A的第i行*AT的第j列。即A的第i行*A的第j行。题意是求矩阵B中非零的Bij有多少个吧。枚举每一个点去跟其他点乘,比如Ai1j1 和 Ai2j2,如果j1==j2.那么Bi1i2就非0.但是这个计数器是个问题啊。比如有a[i].y==a[j].y 还有一对点比如(x1,y1) (x2,y2)。。且(x1==a[i].x)(x2==a[j].x) y1==y2且不等于a[i].y..这样很容易就重复计数啊。也不能用bool型来标记啊。10W*10W的矩阵。果断会MLE啊
解决方法:
二维点排序 把所有的非0点拿粗来排序
排完之后只有相邻的才可能一样啊
也可以用集合来做,方便很多哦。。
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
struct Point
{
int x,y;
}point[1008];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
set <pair<int,int>>st;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
if(point[i].y==point[j].y)
{
st.insert(make_pair(point[i].x,point[j].x));
}
}
}
printf("%d\n",st.size());
}
return 0;
}