Acdream 1213 matrix multiplication (matrix multiplication)

Source: Internet
Author: User
Tags integer numbers time limit
Matrix MultiplicationTime limit:2000/1000ms (java/others) Memory limit:128000/64000kb (java/others) Submit statistic Next problem problem DescriptionLet us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of this graph was nxm matrix A = {A I,j}, such that A i,j are 1 if i-th Vertex is one of the ends of j-t H Edge and 0 in the other case. Your task is to find the sum of all elements of the matrix A TA. InputThe first line of the input file contains the 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 is different and there are no loops (i.e. edge ends is distinct). OutputOutput the only number-the sum requested. Sample Input
4 4
1 2
1 3
2 3
2 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.*;

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 (" 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);}}


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.