ACdream 1213 Matrix Multiplication
来源:互联网 发布:2017下半年网络流行词 编辑:程序博客网 时间:2024/06/06 06:58
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
题意:给一个无向图,设这个图的关联矩阵为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;}
0 0
- ACdream 1213 Matrix Multiplication
- ACdream 1213 Matrix Multiplication
- Acdream 1213——Matrix Multiplication
- ACdream 1213 Matrix Multiplication(矩阵乘法)
- ACdream 1213 Matrix Multiplication(矩阵相乘)
- ACdream 1213 Matrix Multiplication【水题 、 找规律】
- ACdream 1213 Matrix Multiplication 其实是一道思维题
- Matrix Multiplication
- Matrix Multiplication
- Matrix Multiplication
- Matrix Multiplication
- Matrix multiplication
- Matrix multiplication
- acdream--Matrix sum
- acdream 1171 Matrix sum
- Matrix Chain Multiplication
- zoj1094 Matrix Chain Multiplication
- Matrix chain multiplication problem
- 安装minGW编译器及其使用方法
- openssl从PFX导出私钥、公钥
- 两边点连直线求交点总数 树状数组或线段树 poj 3067 Japan
- linux下的 fork vfork和clone函数
- 智力题(农民分金条问题)
- ACdream 1213 Matrix Multiplication
- Activity的启动模式
- HDU 3488--Tour【最小费用最大流 && 有向环最小权值覆盖 && 经典】
- 例题3.11 背单词 LA3942
- odoo(Openerp)如何在字段的on_change方法中动态返回domain
- fork()&&fork()||fork()
- Linux xargs 命令 【转载】
- Merge Two Sorted Lists
- pthread_create函数的详细讲解(包括向线程函数传递参数详解)