I~Matrix(13.7.11)

来源:互联网 发布:手机支付宝软件 编辑:程序博客网 时间:2024/05/22 10:57
Problem I. Matrix
Input le: stdin
Output le: stdout
Time limit: 2 seconds
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 AAT .
Formally, we de ne B = AAT , 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 rst line of input contains a integer C indicating the number
of the cases.
For each test case, the rst 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
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 and output
stdin
2
5 3
1 0
2 1
3 3
3 3
0 0
1 0

2 0

stdout

3

9

此题涉及矩阵的运算,输出为,矩阵中不为0的元素的个数,直接开个矩阵出来会造成超时和内存超标等问题,所以不能那样,通过矩阵转置的意义,可以得知,只要对应列有元素不为0,则这两行所出元素必不为0,

代码:

#include<iostream>using namespace std;int z1[1001],z2[1001],x[1001],y[1001];int main(){int c;cin>>c;while(c--){int n,m;int flag1=1,flag2=0,f2=0;cin>>n>>m;for(int i=0;i<m;i++){cin>>z1[i]>>z2[i];}for(int j=0;j+1<m;j++){if(z1[j]!=z1[j+1]){flag1++;}}for(int a=0;a<m;a++){int f1=0;x[a]==z1[a];for(int h=0;h<=a;h++){if(x[h]==z1[a]){f1++;}}    if(f1<=1){for(int b=0;b<m;b++){if(z1[a]!=z1[b]&&z2[a]==z2[b]){int f2=0;y[b]=z2[b];for(int s=0;s<=b;s++){if(y[s]==z2[b]){f2++;}}if(f2<=1){flag2++;}}}}}cout<<flag1+flag2<<endl;}return 0;}
注意一行出现多个不为0元素的情况


原创粉丝点击