Matrix Multiplication(找規律)

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;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章