ACdream 1213 Matrix Multiplication【水题 、 找规律】

本文介绍了一种快速计算图论中特定矩阵乘法的方法。通过分析无向图的关联矩阵并找出规律,实现了一个高效的算法来计算矩阵乘积的元素之和。

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

Matrix Multiplication

Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 128000/64000KB (Java/Others)
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 = {a i,j}, such that a i,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 A TA.
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

水题。找规律,规律出来,题就解决了。
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define CASE(T)         for(scanf("%d",&T);T--;)
typedef long long LL;
const int maxn = 10000 + 5;
int N, M, cnt[maxn];
int main()
{
//    FIN;
    int a, b;
    while(~scanf("%d %d", &N, &M))
    {
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= M; i++)
        {
            scanf("%d %d", &a, &b);
            cnt[a]++, cnt[b]++;
        }
        LL ans = 0;
        for(int i = 1; i <= N; i++)
        {
            ans += (LL)(cnt[i] * (cnt[i] - 1) / 2);
        }
        ans *= 2;
        ans += 2 * M;
        printf("%lld\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值