ACdream 1213 Matrix Multiplication

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 4
1 2
1 3
2 3
2 4

Sample Output

18

Source

Andrew Stankevich Contest 1

Manager

mathlover

题意:给一个无向图,设这个图的关联矩阵为A。然后计算ATA这个矩阵中,所有元素的和。

关于关联矩阵,百度百科有详细介绍:http://baike.baidu.com/link?url=H9T8Sm0NTuoKwBiKS7nOBFn5oV10M0_UW0XhXAYA2mDjIhq2PUr84sknOaYpYmZocFXDYdp-Kij-RKIIP9rDN_

思路:观察关联矩阵,对于每条边的两个顶点,这条边对结果的贡献是这两个顶点的度,所以只需要扫描每条边,然后加上出现的顶点的度就可以了。

#include <cstring>
#include <cstdio>
typedef long long LL;
const int mx=100010;
int edge[mx][2];
int cnt[mx];

int main() {
    int n,m;
    while(~scanf("%d%d",&n,&m)) {
        memset(cnt,0,sizeof(cnt));
        for(int i=1; i<=m; i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            edge[i][0]=u;
            edge[i][1]=v;
            cnt[u]++;
            cnt[v]++;
        }
        LL ans=0;
        for(int i=1; i<=m; i++) {
            ans+=cnt[edge[i][0]];
            ans+=cnt[edge[i][1]];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 08:47:36

ACdream 1213 Matrix Multiplication的相关文章

ACdream 1213 Matrix Multiplication【水题 、 找规律】

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1213 Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence

ACDream 1213 Matrix Multiplication (01矩阵处理)

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

ACdream 1213 Matrix Multiplication(矩阵相乘)

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

ACdream 1213 Matrix Multiplication(矩阵乘法)

题目链接:http://acdream.info/problem?pid=1213 涉及的数学知识较多,包括矩阵的转置,矩阵的乘法,关联矩阵..... 刚开始是直接按照各个概念做的,结果MLE了,MLE代码如下 #include<cstdio> #include<iostream> #include<sstream> #include<cstdlib> #include<cstring> #include<string> #inclu

acdeream Matrix Multiplication

D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of this graph i

Matrix multiplication

题目链接 题意: 给两个n*n的矩阵,求乘积后对3取摸的结果(1≤n≤800) 分析: 考虑一下为什么给3呢,对3取摸只可能得到0.1.2,都可以看作两位的,那么在乘法的时候我们可以用分配率将原来的矩阵乘法分成四个矩阵乘法,每个矩阵都只包括0和1.对于0/1矩阵的乘法,可以使用bitset来快速运算 const int MAXN = 801; bitset<MAXN> r1[MAXN], r2[MAXN], c1[MAXN], c2[MAXN]; int a[MAXN][MAXN]; int

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];