ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
来源:互联网 发布:东方挂馅炒饭 知乎 编辑:程序博客网 时间:2024/05/28 23:10
Matrix Multiplication
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 aij 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
思路:要求的是A矩阵和A的转置矩阵的积,列一下矩阵会发现,实际上就是求Aij(1<=i<=n,1<=j<=m)的平方的和(这好像是求一个矩阵和它的转置矩阵之积的规律?)。
其实根据线性代数里面的公式,就是矩阵的第K行的和和第K列的和的乘积之和。
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 100010int a[maxn];int n,m;int main(){ int t; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); int x,y; scanf("%d%d",&n,&m); for(int i=1;i<=m;++i) { scanf("%d%d",&x,&y); ++a[x],++a[y]; } int ans=0; for(int i=1;i<=n;++i) ans+=a[i]*a[i]; printf("%d\n",ans); if(t) printf("\n"); } return 0;}
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- Matrix Multiplication(找规律)
- ZOJ2316 Matrix Multiplication(找规律)
- HDU 4920 Matrix multiplication(矩阵乘法)
- hdu 4920 Matrix multiplication (矩阵相乘)
- hdu 4920 Matrix multiplication(矩阵乘法)
- SGU - 196 - Matrix Multiplication (矩阵乘法)
- hdoj Matrix multiplication 4920 (矩阵相乘)
- 【HDU】4920 - Matrix multiplication(矩阵相乘)
- HDU 4920 Matrix multiplication(矩阵优化)
- POJ3318:Matrix Multiplication(矩阵相乘)
- POJ3318 Matrix Multiplication ( 随机化 + 矩阵乘法)
- zoj-1094Matrix Chain Multiplication(栈来实现矩阵相乘)
- 向量、矩阵乘法的几何意义(二) 矩阵乘法(Matrix Multiplication)
- hdu6198(矩阵快速幂+找规律)
- FZU 1911 Construct a Matrix(矩阵快速幂+找规律)
- HDU4920 Matrix multiplication 矩阵
- 矩阵 ACdream1213 Matrix Multiplication
- win10 下xgboost的安装----终极版
- STL-管道二
- [LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
- HTML5之drawImage函数
- 定时器,延时函数
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- spring mvc +ibatis 1
- JUnit初学
- 学生信息的添加与查询(java集合框架应用)
- bzoj1145: [CTSC2008]图腾totem
- MFC错误记录-error C2501
- The Begining
- 邻接链表与图及其相关操作--Java
- 微服务指南走北(四):你不愿意做微服务架构的十个理由