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 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

题意:给出一个N个点个M条无向边的关系构成一个01矩阵,求01矩阵A*A^T后新矩阵的每个元素的值。

分析:这个邻接矩阵的转置矩阵还是这个矩阵本身,那么题目就变成了求A^2,有1W个点的话,用普通的矩阵肯定存不下来的,会超内存。

设原矩阵为A,矩阵相乘之后为B,转置之后矩阵还是本身,按照矩阵乘法本来是A的第 i 行乘第 j 列得到 Aij, 转变思路思考一下,可以变成第 Aij 可以变成每一行和该行本身相乘。

可以先计算出每一列的1的个数用一维数组C存下来,然后如果Bij=1的话,就Bij*C[i],可以纸上模拟一下。

换种说法,就是先存下每个点的度,然后把所有边的两边的点的度相加。

数据可能会超int,用long long

#pragma comprint(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#define LL __int64
#define FIN freopen("in.txt","r",stdin)
using namespace std;
const int MAXN=10000+5;
int cnt[MAXN];
struct node
{
    int x,y;
}edge[100000+5];
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            cnt[u]++;
            cnt[v]++;
            edge[i].x=u;
            edge[i].y=v;
        }
        long long ans=0;
        for(int i=0;i<m;i++)
        {
            int u=edge[i].x;
            int v=edge[i].y;
            ans+=cnt[u];
            ans+=cnt[v];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

时间: 2024-12-31 06:13:15

ACDream 1213 Matrix Multiplication (01矩阵处理)的相关文章

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【水题 、 找规律】

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

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

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)

题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据是不可能会有的,而我们一开始一直在想极端数据能接受的方法......后来看了鹏哥的做法,就是把是0的地方都跳过就可以了,用矩阵保存前一个非0数的位置是多少.二师兄给我看了一个代码,人家根本没用别的优化,直接将最里层k的循环提到了最外层,然后就AC了,对此我表示无语. 1 #include <cstd

hdu 4920 Matrix multiplication(矩阵相乘)多校训练第5场

Matrix multiplication                                                                           Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Given two matrices A and B of size n×n, find the

hdu 4920 Matrix multiplication (矩阵乘法)

Problem Description Given two matrices A and B of size n×n, find the product of them.bobo hates big integers. So you are only asked to find the result modulo 3. Input The input consists of several tests. For each tests:The first line contains n (1≤n≤

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matrices A and B of size n×n, find the product of them. b