Matrix multiplicationTime limit:2000/1000ms (java/others)Memory limit:128000/64000kb (java/others)Submit statistic Next Problemproblem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of this graph is nxm matrix A = {ai,j}, such that ai,jis 1 if i-th vertex are one of the ends of the 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 the 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 is different and there are no loops (i.e. edge ends is distinct). Output output the only number-the sum requested. Sample Input
4 41 21 32 32 4
Sample Output
18
Actually is a matrix, oneself by oneself, drawing can understand the second
java:
import java.util.*;import Java.io.*;import java.math.*;p ublic class main{static final int maxn = 10000 + 5;static int [] MP = new INT[MAXN];p ublic static void Main (S Tring [] agrv) throws Ioexception{//system.setin (New FileInputStream ("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);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Acdream 1213 matrix multiplication (matrix multiplication)