Matrix Multiplication(找规律)

本文介绍了一道关于图论的问题,通过构建图的关联矩阵并计算该矩阵与其转置矩阵乘积的元素和来解决。问题涉及图的表示、矩阵运算及高效的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matrix Multiplication

Time Limit: 2 Seconds     Memory Limit: 32768 KB

Let us consider undirected graph G = which has N vertices and M edges. Incidence matrixof this graph is N * M matrix A = {aij}, such that aij is 1 if i-th vertex is one of the ends of j-th edgeand 0 in the other case. Your task is to find the sum of all elements of the matrix ATA.


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


题解:T组数据,n,m,然后m行边,根据m行边转换成一个n行m列的矩阵,求这个矩阵的转置矩阵乘以此矩阵的出来的矩阵的元素之和……

比赛的时候一看题目,矩阵相乘,一脸懵逼,不会,赶紧找了本线代书看了下,看了半个小时终于懂了,但一看数据范围,我了个大草,这怎么存图,怎么运算,肯定妥妥的双超……

赛后看了下别人的的代码……我心中一万只国宝奔腾而过……不过还是没弄懂什么意思,如果哪位大神知道,请拯救一下迷途的羔羊……



#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<string>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define ll long long
#define For(i,a,b) for(int i=a;i<b;i++)
#define sf(a)  scanf("%d",&a)
#define sfs(a)  scanf("%s",a)
#define sff(a,b)  scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pf(a) printf("%d\n",a)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int x[10005];
int main()
{
    int t,n,m,falg=0;
    sf(t);
    while(t--)
    {
        if(falg)printf("\n");falg=1;
        mem(x,0);
        sff(n,m);
        int a,b;
        For(i,0,m)
        {
            sff(a,b);
            x[a]++;x[b]++;
        }
        ll s=0;
        For(i,1,n+1)
        {
            s+=x[i]*x[i];
        }
        cout<<s<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值