ACDream 1213 Matrix Multiplication (01矩阵处理)
2015-08-26 22:16
489 查看
Matrix Multiplication
Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
[align=center]Submit Statistic Next Problem[/align]
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; }
View Code
相关文章推荐
- 自动提示和手动提示 VS2013与Eclipse中的设置
- leetcode 258 Add Digits(难易度:Easy)
- 笑话类android项目总结
- PS学习笔记----图层锁定
- 3-06. 表达式转换(25)
- 后缀数组经典题---不可重叠最长重复子串 poj1743
- 深入理解”引用被初始化后,不能再指向其他对象“这句话?
- JS学习之prototype属性
- 几道python笔试题
- Word Loadder II (***) -- BFS DFS
- 慕课学习C++笔记02-封装(上)
- Functional MRI (second edition) -- 10. Statistical Analysis:Basic Analyses
- QT---qtextedit中的光标问题
- QT---设置textedit文本框中某个字符格式
- 【转】vxworks的default boot line说明
- FFMPEG基于内存的转码实例——输入输出视频均在内存
- 华为OJ(笔画)
- 九度oj 1152
- Note For Linux By Jes(12)-认识系统服务(daemons)
- java导出word文档