ZOJ 2316 Matrix Multiplication

Matrix Multiplication

Time Limit: 2000ms

Memory 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个的组合数。。。。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 10010;
18 int d[maxn];
19 int main() {
20     int t,u,v,n,m,ans;
21     scanf("%d",&t);
22     while(t--){
23         scanf("%d %d",&n,&m);
24         memset(d,0,sizeof(d));
25         for(int i = 0; i < m; i++){
26             scanf("%d %d",&u,&v);
27             ++d[u];
28             ++d[v];
29         }
30         ans = 0;
31         for(int i = 1; i <= n; i++)
32             ans += d[i]*(d[i]-1)/2;
33         ans = (ans + m)<<1;
34         printf("%d\n",ans);
35         if(t) puts("");
36     }
37     return 0;
38 }

时间: 2024-10-06 11:56:43

ZOJ 2316 Matrix Multiplication的相关文章

zoj 2316 Matrix Multiplication 解题报告

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2316 题目意思:有 N 个 点,M 条 边.需要构造一个N * M 大小的矩阵A.对于 (i, j) 这个坐标点它表示,对编号为 i 这个点编号为 j 的 点与它相连,此时标记(i, j) 为1,如果坐标点没有跟这条 j 的边相连,就标记为0.构造完这个矩阵A之后,需要求出它的转置矩阵AT,算出 ATA 的和. 新学期第一场比赛!刚开始真是打算直接做的,但是数据

ACDream 1213 Matrix Multiplication (01矩阵处理)

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of

Matrix multiplication

题目链接 题意: 给两个n*n的矩阵,求乘积后对3取摸的结果(1≤n≤800) 分析: 考虑一下为什么给3呢,对3取摸只可能得到0.1.2,都可以看作两位的,那么在乘法的时候我们可以用分配率将原来的矩阵乘法分成四个矩阵乘法,每个矩阵都只包括0和1.对于0/1矩阵的乘法,可以使用bitset来快速运算 const int MAXN = 801; bitset<MAXN> r1[MAXN], r2[MAXN], c1[MAXN], c2[MAXN]; int a[MAXN][MAXN]; int

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

[SGU 196] Matrix Multiplication

196. Matrix Multiplication time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard output: standard Description Let us consider an undirected graph G = <V, E> which has N vertices and M edges. Incidence matrix of this graph is

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matrices A and B of size n×n, find the product of them. b

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640