Matrix--集合--点二维排序

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
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.
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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值