Zoj 2316 matrix multiplication solution report

Source: Internet
Author: User

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

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.