Matrix Multiplication(找规律)

来源:互联网 发布:怕死 陈奕迅 知乎 编辑:程序博客网 时间:2024/05/22 00:51
Matrix Multiplication

Time Limit: 2 Seconds     Memory Limit:32768 KB

Let us consider undirected graph G = which has N vertices and M edges. Incidence matrixof this graph is N * M matrix A = {aij}, such that aij is 1 if i-th vertex is one of the ends of j-th edgeand 0 in the other case. Your task is to find the sum of all elements of the matrix ATA.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

The first line of the input file contains two integer numbers - N and M (2 <= N <= 10 000, 1 <= M <= 100 000). 2M integer numbers follow, forming M pairs, each pair describes one edge of the graph. Alledges are different and there are no loops (i.e. edge ends are distinct).


Output

Output the only number - the sum requested.


Sample Input

1

4 4
1 2
1 3
2 3
2 4


Sample Output

18


题解:T组数据,n,m,然后m行边,根据m行边转换成一个n行m列的矩阵,求这个矩阵的转置矩阵乘以此矩阵的出来的矩阵的元素之和……

比赛的时候一看题目,矩阵相乘,一脸懵逼,不会,赶紧找了本线代书看了下,看了半个小时终于懂了,但一看数据范围,我了个大草,这怎么存图,怎么运算,肯定妥妥的双超……

赛后看了下别人的的代码……我心中一万只国宝奔腾而过……不过还是没弄懂什么意思,如果哪位大神知道,请拯救一下迷途的羔羊……



#include<bits/stdc++.h>#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>#include<stdlib.h>#include<time.h>#include<string>#include<math.h>#include<map>#include<queue>#include<stack>#define INF 0x3f3f3f3f#define ll long long#define For(i,a,b) for(int i=a;i<b;i++)#define sf(a)  scanf("%d",&a)#define sfs(a)  scanf("%s",a)#define sff(a,b)  scanf("%d%d",&a,&b)#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)#define pf(a) printf("%d\n",a)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int x[10005];int main(){    int t,n,m,falg=0;    sf(t);    while(t--)    {        if(falg)printf("\n");falg=1;        mem(x,0);        sff(n,m);        int a,b;        For(i,0,m)        {            sff(a,b);            x[a]++;x[b]++;        }        ll s=0;        For(i,1,n+1)        {            s+=x[i]*x[i];        }        cout<<s<<endl;    }}


0 0
原创粉丝点击