您的位置:首页 > 其它

ZOJ - 2316 Matrix Multiplication

2017-04-18 19:13 381 查看
Let us consider undirected graph G = which has N vertices and M edges. Incidence matrix of this graph is N * M matrix A = {aij}, 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 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. All edges 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

题意:就是n个点,m条边,建成一个矩阵,问这个
4000
矩阵和他的转置矩阵的乘积。

思路:例如样例,建成的矩阵是  0 1 1 0

                                                      1 0 1 1

                                                      1 1 0 0

                                                      0 1 0 0

乘以它的转置相当于乘它本身,a[1][2]这个数要和a[2][1],a[2][3],a[2][4]相乘得3个1,a[3][2],a[4][2]同理

每个矩阵位置为1的都可以这样计算,得出规律了

#nclude<iostream>

#include<cstdio>
#include<cstring>
using namespace std;
int main(){
int a[10005];
int t,n,m;
int x,y,i;
scanf("%d",&t);
while(t--){
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&x,&y);
a[x]++;
a[y]++;
}
int sum=0;
for(i=1;i<=n;i++)
sum+=a[i]*a[i];
if(t==0)
printf("%d\n",sum);
else
printf("%d\n\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM