Codeforces Round #277.5 (Div. 2) D

D. Unbearable Controversy of Being
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!

Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections abcand d, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a, b)(b, c)(a, d)(d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:

Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.

Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.

When rhombi are compared, the order of intersections b and d doesn't matter.

Input

The first line of the input contains a pair of integers nm (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000) — the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi) — the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.

It is not guaranteed that you can get from any intersection to any other one.

Output

Print the required number of "damn rhombi".

Sample test(s)
input
5 4
1 2
2 3
1 4
4 3
output
1
input
4 12
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
output
12
 
        
         
         
题意:给你n代表多少个点,和m条边,然你求存在多少个菱形,即图所示。
做法:先用二维数组记录各个点之间的关系,然后求出存在多少个a-b-c这样的半菱形记为n,那么能组成的菱形就是c(n,2),最后答案累加就是最后的答案。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
LL dp[505][505];
int num[505][505],a[505];
int main()
{
    int n,m,mod;
    int i,j;
    char ss[1005];
    while(scanf("%d%d%d",&n,&m,&mod)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        memset(num,0,sizeof(num));
        getchar();
        for(i=1;i<=m;i++)
        {
            gets(ss);
            for(j=0;j<n;j++)
                num[i][j+1]=ss[j]-'0';
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(num[j][i]!=0)
                    a[i]++;
            }
        }
        int x,y;
        x=0;
        y=0;
        for(i=1;i<=n;i++)
        {
            if(a[i]==2)
                x++;
            if(a[i]==1)
                y++;
        }
        dp[x][y]=1;
        for(i=x;i<=n;i++)
            for(j=0;j<=n-i;j++)
            if(dp[i][j])
            {
                dp[i][j+2]+=(n-i-j)*(n-i-j-1)/2*dp[i][j]%mod;
                dp[i+1][j]+=j*(n-i-j)*dp[i][j]%mod;
                if(j>=2)
                dp[i+2][j-2]+=j*(j-1)/2*dp[i][j]%mod;
            }
        printf("%I64d\n",dp[n][0]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值