HUST 1615 Matrix

来源:互联网 发布:商超软件 编辑:程序博客网 时间:2024/05/16 06:26

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 A*AT.
Formally, we define B = A*AT, A(i,j) 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 A(x,y) is 1.

N <= 100,000 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.

题意:求B中非0的个数

#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <vector>#include <set>#include <map>using namespace std;struct node{    int x,y;    bool operator < (const node &a)const    {        if(a.x!=x)         return a.x>x;        return a.y>y;    }}pi[1007];map<node,int> q;int main(){    int t;    cin>>t;    while(t--)    {        int n,m;        cin>>n>>m;        q.clear();        for(int i=0;i<m;i++)            cin>>pi[i].x>>pi[i].y;        int cnt=0;        for(int i=0;i<m;i++)        {            for(int j=0;j<m;j++)            {                node tmp;                tmp.x=pi[i].x,tmp.y=pi[j].x;                if(pi[i].y==pi[j].y&&q[tmp]==0)                {                    q[tmp]=1;                    cnt++;                }            }        }        cout<<cnt<<endl;    }}
0 0
原创粉丝点击