ACdream 1213 Matrix Multiplication(矩阵相乘)

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 this graph is N × M matrix A = {ai,j}, such
that ai,j 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.

Input

The first line of the input file contains two integer numbers — N and M (2 ≤ N ≤ 10 000, 1 ≤ M ≤100 000). Then 2*M 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

4 4
1 2
1 3
2 3
2 4

Sample Output

18
其实就是一个矩阵,自己乘自己,画图可以秒懂
java:
import java.util.*;
import java.io.*;
import java.math.*;

public class Main{
	static final int MAXN = 10000 + 5;
	static int [] mp = new int[MAXN];
	public static void main(String [] agrv)
	throws IOException
	{
		//System.setIn(new FileInputStream(new File("D:" + File.separator + "imput.txt")));
		Scanner cin = new Scanner(System.in);
		int N, M, u, v;
		while(cin.hasNext()){
			N = cin.nextInt();
			M = cin.nextInt();
			Arrays.fill(mp, 1, N + 1, 0);
			for(int i = 1;i <= M ;i ++){
				u = cin.nextInt();
				v = cin.nextInt();
				if(u == v) mp[v] ++;
				else {
					mp[v] ++;
					mp[u] ++;
				}
			}
			long ans = 0;
			for(int i = 1;i <= N;i ++){
				ans += (long)mp[i] * mp[i];
			}
			System.out.println(ans);
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-09 03:33:39

ACdream 1213 Matrix Multiplication(矩阵相乘)的相关文章

ACdream 1213 Matrix Multiplication(矩阵乘法)

题目链接:http://acdream.info/problem?pid=1213 涉及的数学知识较多,包括矩阵的转置,矩阵的乘法,关联矩阵..... 刚开始是直接按照各个概念做的,结果MLE了,MLE代码如下 #include<cstdio> #include<iostream> #include<sstream> #include<cstdlib> #include<cstring> #include<string> #inclu

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 

HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵

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

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

ACdream 1213 Matrix Multiplication【水题 、 找规律】

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1213 Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence

ACdream 1213 Matrix Multiplication

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

HDU4920 Matrix multiplication 矩阵

不要问我 为什么过了 窝也不造为什么就过了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include<iostream> #inclu

hdu 4920 Matrix multiplication(矩阵坑题)

http://acm.hdu.edu.cn/showproblem.php?pid=4920 被这道题虐了一下午,啥也不说了.继续矩阵吧. 超时就超在每步取余上,要放在最后取余,再者注意三个循环的次序. #include <stdio.h> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <cma

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam