D

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
",sum);
            if(t)
                printf("
");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/nyist-xsk/p/7264817.html