Codeforces 469D Unbearable Controversy of Being

题目链接:http://codeforces.com/problemset/problem/489/D


题意:给出一个有向图,求有多少个如下的棱形(2个点之间可以有多个棱形)

Codeforces 469D Unbearable Controversy of Being_第1张图片


思路:枚举2个点A,B然后找出所有与A相连的点是否与B相,相连则s++,这2个点之间的棱形就是s*(s-1)/2


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 3030
using namespace std;

int G[maxn][maxn];
vector <int> link[maxn];


int main()
{
    int n,m;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        memset(G,0,sizeof(G));
        memset(link,0,sizeof(link));
        for (int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u][v]=1;
            link[u].push_back(v);
        }

        int res=0;
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if (i==j) continue;
                int sum=0;
                for (int k=0;k<link[i].size();k++)
                {
                    int v=link[i][k];
                    if (G[v][j]==1) sum++;
                }
                res+=sum*(sum-1)/2;
            }
        }
        printf("%d\n",res);
    }
}



你可能感兴趣的:(ACM,codeforces)