Let us consider undirected graph G = which has N vertices and M edges. Incidence matrix of this graph is N * M matrix A = {a ij}, such that a ij is 1 if i-th vertex is one of the ends of j-th edge and 0 in the other case. Your task is to find the sum of all elements of the matrix A TA.
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.
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. All edges are different and there are no loops (i.e. edge ends are distinct).
Output the only number - the sum requested.
1
4 4
1 2
1 3
2 3
2 4
18
题意:给你一个矩阵,初始为0,1 2代表矩阵A12和A21位置的值为1,求逆反矩阵和原矩阵的乘积和。
分析:线性代数公式 , 行列乘积之和就是答案。就是矩阵的第K行和,第K列和乘积之和。
代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int du[10005];
LL C(int Count){
return Count*(Count-1)/2;
}
int main(){
int test,cur=0;
LL ans=0;
while(~scanf("%d",&test)){
while(test--){
if(cur)cout<<endl;
else cur=1;
int n,m,x,y;
cin>>n>>m;
long long a[11000];
memset(a,0,sizeof(a));
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
a[x]++;
a[y]++;
}
long long sum=0;
for(int i=1;i<=n;i++)sum+=a[i]*a[i];
cout<<sum<<endl;
}