Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2316.
Meaning: There are n vertices and m edges. We need to construct a n * M matrix. For the coordinate point (I, j), it indicates that the vertex numbered as I is connected to the vertex numbered as J. At this time, Mark (I, j) as 1, if the coordinate point is not connected to the edge of this J, it is marked as 0. After constructing this matrix A, you need to find its transpose matrix at and calculate the sum of ATA.
The first match in the new semester! It was originally intended to be done directly, but the data was too big. 2 <= n <= 10 000, 1 <= m <= 100 000, which can only be simplified through observation, after more than three hours, I finally came up with =! I'm so touched! Supplemented the knowledge of the matrix ......
The rule can be found only after manual calculation !!! Later, I made a low-level mistake and forgot to clear it. Every test is required. Finally, there is a format problem. An empty line is output between each output.
Paste a code to commemorate it.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <cstring> 5 using namespace STD; 6 7 typedef long ll; 8 const int n = 10000 + 2; 9 10 ll a [n], ans; 11 12 INT main () 13 {14 int t, n, m, V1, V2; 15 while (scanf ("% d", & T )! = EOF) 16 {17 while (t --) 18 {19 int Maxi =-1; 20 scanf ("% d", & N, & M ); 21 memset (A, 0, sizeof (a); 22 for (INT I = 1; I <= m; I ++) 23 {24 scanf ("% d", & V1, & V2); 25 A [V1] ++; // calculate the number of edges attached to each vertex. 26 A [V2] ++; 27 int TM = max (V1, V2); 28 Maxi = max (Maxi, Tm ); // obtain the maximum vertex number 29} 30 ans = 0; 31 for (INT I = 1; I <= Maxi; I ++) 32 {33 if (a [I]) 34 ans + = A [I] * A [I]; 35} 36 printf ("% LLD \ n", ANS ); 37 If (t) 38 puts (""); 39} 40} 41 return 0; 42}
Zoj 2316 matrix multiplication solution report