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