ZOJ 2316 Matrix Multiplication
2014-09-29 17:02
375 查看
Matrix Multiplication
Time Limit: 2000msMemory Limit: 32768KB
This problem will be judged on ZJU. Original ID: 2316
64-bit integer IO format: %lld Java class name: Main
Let us consider undirected graph G = <v, e="">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
Source
Andrew Stankevich's Contest #1解题:题意转化后,就是计算途中有多少条,长度为2的路径。注意是无向图。。。。每个顶点,看它的度是多少。从这些度里面选取2个的组合数。。。。。。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 10010; int d[maxn]; int main() { int t,u,v,n,m,ans; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&m); memset(d,0,sizeof(d)); for(int i = 0; i < m; i++){ scanf("%d %d",&u,&v); ++d[u]; ++d[v]; } ans = 0; for(int i = 1; i <= n; i++) ans += d[i]*(d[i]-1)/2; ans = (ans + m)<<1; printf("%d\n",ans); if(t) puts(""); } return 0; }
View Code
相关文章推荐
- zoj 2316 Matrix Multiplication(D)
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- zoj 2316 Matrix Multiplication(2-D)
- zoj 2316 Matrix Multiplication(2-D)
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ2316——Matrix Multiplication
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- zoj 2316 Matrix Multiplication
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ - 2316 Matrix Multiplication(模拟)
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- D - Matrix Multiplication ZOJ - 2316 规律题
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ-2316-鄙视浙大的OJ
- ZOJ - 2316 Matrix Multiplication
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ 2316 Matrix Multiplication
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)
- ZOJ 2316 Matrix Multiplication(找规律)(矩阵和它的转置矩阵之积)