ACdream 1213 简单数学

来源:互联网 发布:纬创软件 福州 编辑:程序博客网 时间:2024/05/20 15:41

ACdream 1213

Matrix Multiplication

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next Problem

Problem Description

      Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of this graph is N × M matrix A = {ai,j}, such that ai,j 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 ATA.

Input

      The first line of the input file contains two 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 are different and there are no loops (i.e. edge ends are distinct).

Output

      Output the only number — the sum requested.

Sample Input

4 41 21 32 32 4

Sample Output

18

Source

Andrew Stankevich Contest 1

Manager

mathlover

状态转移规律正好符合矩阵乘法

#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <cstdio>using namespace std;int main(){    int n,m,x,y;    cin>>n>>m;    long long a[10005]={0};    long long ans=0;    for(int i=0;i<m;i++){        cin>>x>>y;        a[x]++;a[y]++;    }    for(int i=1;i<=n;i++){        ans+=a[i]*a[i];    }    cout<<ans<<endl;}


0 0