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/ ...
随机推荐
- 基于 Docker 的 Zabbix 微服务系统
zabbix 官网提供一个镜像 [ zabbix-appliance ], 可以直接拉起一个 zabbix-server. 但是数据库无法分离出来. 本实践使用 zabbix 官方提供的 Docker ...
- html+js+node实现五子棋线上对战,五子棋最简易算法
首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...
- No 'Access-Control-Allow-Origin' Ajax跨域访问解决方案
No 'Access-Control-Allow-Origin' header is present on the requested resource. 当使用ajax访问远程服务器时,请求失败,浏 ...
- arm---先搞清楚各种版本号【转】
转自:http://blog.csdn.net/linnoo/article/details/53214689 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] ARM的几种版本 ...
- MVC使用Newtonsoft无需实体类,实现JSON数据返回给前端页面使用
//引用using Newtonsoft.Json; using Newtonsoft.Json.Linq; public ActionResult JsonSample() { ResponseRe ...
- 广度优先搜索--POJ迷宫问题
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- 【UOJ#164】清华集训2015V
QwQzcysky真是菜死了,这是我刚上高一的时候坤爷在夏令营讲的,可是今天才切掉…… 想想也神奇,一个2016.11才学会线段树的菜鸡,夏令营的时候居然听过Segment-Tree-Beats? 所 ...
- VPS性能综合测试(5):UnixBench工具介绍
UnixBench 介绍 UnixBench 是一个类 unix (Unix, BSD, Linux 等) 系统下的性能测试工具,它是一个开源工具.可以用于测试系统主机的性能. UnixBench 进 ...
- C语言string转int
再不用atoi函数接口时,将字符串型转换成整型. 例如:将字符串型“158”转换成整型158 int String2Int(char * buff) { ; ; '; index++) { value ...
- find命令的基本用法
linux 中find 常用示例解析 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression] 其实[-H] [-L] [- ...