D - Matrix Multiplication ZOJ - 2316 规律题
Let us consider undirected graph G = which has N vertices and M edges. Incidence matrix of this graph is N * M matrix A = {a ij}, such that a ij 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 A TA.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line of the input file contains two integer numbers - N and M (2 <= N <= 10 000, 1 <= M <= 100 000). 2M 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
1
4 4
1 2
1 3
2 3
2 4
Sample Output
18
要注意多组输入之下加n组输入,而且zoj不支持%I64d所以只能用%lld
题意就是给你n,m代表n个顶点m条边,然后m行每行俩数代表x到y有一条边
所以在矩阵里面如果有哪两个点之间有一条边则为1否则为0,然后求矩阵与其转置矩阵的乘积,
而且是无向边,看似很复杂的一道题但是如果把矩阵及其转置矩阵给列出来的话会发现,这是一种矩阵的特殊情况,即矩阵和它的转置矩阵是对称的,那么再求积的话,相当于求某个顶点的平方和,
对于每组顶点的输入用数组记录每个顶点出现的次数,然后把每个顶点的平方和加起来就行了
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
typedef long long ll;
const int maxn=10005;
const int INF=0x3f3f3f3f;
ll t,n,m,x,y;
ll a[maxn];
int main()
{
while(~scanf("%lld",&t))
{
while(t--)
{
ll i;
memset(a,0,sizeof(a));
scanf("%lld %lld",&n,&m);
while(m--)
{
scanf("%lld %lld",&x,&y);
a[x]++;
a[y]++;
}
ll sum=0;
for(i=1; i<=n; i++) sum+=a[i]*a[i];
printf("%lld\n",sum);
if(t)
printf("\n");
}
}
return 0;
}
D - Matrix Multiplication ZOJ - 2316 规律题的更多相关文章
- ZOJ 2316 Matrix Multiplication
Matrix Multiplication Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. O ...
- 矩阵乘法 --- hdu 4920 : Matrix multiplication
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- hdu4920 Matrix multiplication 模3矩阵乘法
hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场
Matrix multiplication Time ...
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
- 【数学】Matrix Multiplication
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total S ...
- hdu 4920 Matrix multiplication bitset优化常数
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- acdeream Matrix Multiplication
D - Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...
- HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
随机推荐
- MySQL 基于 GTID 主从架构添加新 Slave 的过程
内容全部来自: How to create/restore a slave using GTID replication in MySQL 5.6 需求说明 需求: 对于已经存在的 MySQL 主从架 ...
- 01背包入门 dp
题目引入: 有n个重量和价值分别为Wi,Vi的物品.从这些物品中挑选出总重量不超过W的物品,求所有挑选方案中的价值总和的最大值. 分析: 首先,我们用最普通的方法,针对每个物品是否放入背包进行搜索. ...
- webpack自动化构建你的项目
1.读万卷书,行万里路. 2.书山有路勤为径,学海无涯苦作舟. 技术段: 相信很多刚接触前端的小伙伴,对一些自动化工具会感觉无可下手.现在前端的发展的势头,势必和后台形成一个对立面,独挡一面. 这篇文 ...
- 《Linux内核原理与设计》第十一周作业 ShellShock攻击实验
<Linux内核原理与设计>第十一周作业 ShellShock攻击实验 分组: 和20179215袁琳完成实验及博客攥写 实验内容: Bash中发现了一个严重漏洞shellshock, ...
- 2017-2018-1 20179205《Linux内核原理与设计》第九周作业
<Linux内核原理与设计>第九周作业 视频学习及代码分析 一.进程调度时机与进程的切换 不同类型的进程有不同的调度需求,第一种分类:I/O-bound 会频繁的进程I/O,通常会花费很多 ...
- python自动开发之第二十五天
一.组合搜索 参考: http://www.cnblogs.com/ccorz/p/5985205.html 二.JSONP 1.在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,但 ...
- 自动化测试===Macaca环境搭建,自我总结
安装jdk 安装安卓sdk(打开sdk的时候出现问题linux===启动sdk manager下载配置sdk的时候报错的解决办法) 安装gradle,配置环境变量(MACACA===gradle下载和 ...
- sicily 1020. Big Integer
Description Long long ago, there was a super computer that could deal with VeryLongIntegers(no VeryL ...
- 设计模式之笔记--抽象工厂模式(Abstract Factory)
抽象工厂模式(Abstract Factory) 定义 抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 类图 描述 多个抽象产品 ...
- linux sort排序命令
1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. 2 sort的-u选项 在输出行中去除重复行. $ ...