[SGU 196] Matrix Multiplication

196. Matrix Multiplication

time limit per test: 0.25 sec. 
memory limit per test: 65536 KB
input: standard 
output: standard

Description

Let us consider an undirected graph G = <V, E> which has N vertices and M edges. Incidence matrix of this graph is an N × M matrix A = {a ij}, such that a ij is 1 if i-th vertex is one of the ends of j-th edge and 0 in the other case. Your task is to find the sum of all elements of the matrix A TA where A T is A transposed, i.e. an M × N matrix obtained from A by turning its columns to rows and vice versa. 

Input

The first line of the input file contains two integer numbers — N and M (2 le N le 10,000, 1 le M le 100,000). 2M integer numbers follow, forming M pairs, each pair describes one edge of the graph. All edges are different and there are no loops (i.e. edge ends are distinct). 

Output

Output the only number — the sum requested. 

Sample test(s)

Input
4 4 
1 2 
1 3 
2 3 
2 4 
Output
18 
 
【题解】

所以,我们就可以得到下代码:

 1 #include<stdio.h>
 2 using namespace std;
 3 int sum[100001],n,m;
 4 long long ans=0;
 5 int main() {
 6     scanf("%d%d",&n,&m);
 7     for (int i=1;i<=m;++i) {
 8         int u,v;scanf("%d%d",&u,&v);sum[u]++;sum[v]++;
 9     }
10     for (int i=1;i<=n;++i) ans+=(long long)sum[i]*sum[i];
11     printf("%I64d\n",ans);
12     return 0;
13 }
View Code

顺带介绍下vjudge,虚拟测评

http://acm.hust.edu.cn/vjudge/toIndex.action

posted @ 2015-06-24 20:29  TonyFang  阅读(364)  评论(0编辑  收藏  举报