博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Matrix
阅读量:7082 次
发布时间:2019-06-28

本文共 2257 字,大约阅读时间需要 7 分钟。

Problem G: Matrix

Time Limit: 2 Sec  
Memory Limit: 128 MB
Submit: 80  
Solved: 11

Description

To efficient calculate the multiplication of a sparse matrix is very useful in industrial filed. 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 AA
T.
Formally, we define B = AA
T, 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

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 Axyis 1.
N ≤ 100000, M ≤ 1000, C ≤ 10

 

Output

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

25 31 02 13 33 30 01 02 0

Sample Output

39

HINT

 

A
Tmeans the Transpose of matrix A, for more details, A
Tij= Aji.
eg:
if Matrix A is:
123
456
789
then the matrix ATis
147
258
369
思路:这个题真是做了好久,虽然不是很难,但想法的不同的确会影响做题的结果。
还有就是开始的时候题目数据有点水,后来改了数据就没能通过,又做了好久才搞出来。
我把这个过程的经历都说一下:
题目的意思就是求一个矩阵(元素为1或0)乘以它的转置矩阵,求结果矩阵的元素有多少个不为0,因为数据比较大(100000),直接用数组保持是不现实的,并且也不能运算。开始想到一种方法,实质上b[i][j]=a矩阵的第i行*第j行,而由矩阵的乘法
b[i][j]=a[i][k]*a'[k][j]+...
也就是说k值相等的情况下,如果a[i][k]与a'[k][j]都为1,那么b[i][j]一定不为0
例如
原矩阵   0 0          0 0 矩阵逆
             1 0          0 1
             2 1          1 2
k值相等,可以看出是0或1,当为0时,可以得出(0 0)(1 1)这两个元素不为0,当为1时,(2 2)不为0
但这样会有重复的现象,如
原矩阵   0 0          0 0 矩阵逆
             0 1          1 0
             2 1          1 2
这样得出的点有(0 0)(0 0)(0 2)(2 2)
出现了计算重复的点,必须把这些点减去
于是,出现了下面的代码:
#include
#include
#include
using namespace std;#define MAX 1000int x[MAX+10],y[MAX+10];int main(){ //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int num,i,j,n,m,ans; scanf("%d",&num); while(num--) { scanf("%d%d",&n,&m); for(i=0;i
提交的时候是过了,但后来发现还有重复的,如
原矩阵   3 1          1 3 矩阵逆
             3 2          2 3
             5 1          1 5
             5 2          2 5
点(3 5)和(5 3)重复了,但不是上面那种形式的重复。
于是改了数据,于是...这种方法就做不了了。
下面说另一种思路,只加,但没有重复的
用MAP做就很简单了。。。。。。。
代码:
#include
#include
#include
#include
#include
#include
using namespace std;struct node{ int x,y;}p[1010];map
mymap;int cmp(node a,node b){ return a.x

 

转载地址:http://fzlml.baihongyu.com/

你可能感兴趣的文章
多级菜单系统安装维护shell脚本实现企业级案例
查看>>
那些年,我玩过的操作系统
查看>>
Lync Server 2013标准版升级Skype for Business Server 2015实战(上)
查看>>
新浪、万网前系统架构师高俊峰:统一监控报警平台架构设计思路
查看>>
2011-9-25俱乐部活动
查看>>
JMeter正则表达式提取器
查看>>
Nginx
查看>>
How To Enable‘root’Account Login Solaris 11 Directly
查看>>
MongoDB学习初步
查看>>
sccm 2007 r2 step by step 之十二 操作系统分发part1
查看>>
Tokyo Tyrant基本规范(1)--介绍和安装
查看>>
实验搭建 GitLab 平台
查看>>
oracle 11g dataguard环境搭建
查看>>
Puppet 实验六 多环境配置
查看>>
项目实战:LAMP环境+Xcache+Redis,另附Memcached配置。
查看>>
ORA-30036故障处理思路
查看>>
WINDOWS7更改访问windows共享的用户名和密码
查看>>
Advanced Threat Analytics 2016
查看>>
SFB 项目经验-31-批量为n台服务器导入PFX证书
查看>>
0-Microsoft Lync Server 2010-部署
查看>>