Acdream 1213 matrix multiplication (matrix multiplication)

Source: Internet
Author: User
Tags integer numbers

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.