Codeforces Round #277.5(Div. 2) D. Unbearable Controversy of Being【暴力】

来源:互联网 发布:怎么做数据分析 编辑:程序博客网 时间:2024/05/21 07:06

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 intersectionsa, b,c and d, such that there are two paths froma to c — one throughb 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 andm 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 andd doesn't matter.

Input

The first line of the input contains a pair of integers n, m (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000) — the number of intersections and roads, respectively. Nextm lines list the roads, one per line. Each of the roads is given by a pair of integersai, 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".

Examples
Input
5 41 22 31 44 3
Output
1
Input
4 121 21 31 42 12 32 43 13 23 44 14 24 3
Output
12

题目大意:

找到四个点,形状和题中图一模一样.问一共能够组成多少组这样的4个点。


思路:


1、一开始想的是O(m^2)枚举两条边,然后判断四个点之间的边的形状是否和要求一样即可。

但是因为m比较大,还是TLE了.(CF冲1e9也是可以尝试的);


2、其实因为N比较小,我们直接O(n)枚举起点,然后暴力深搜长度为2的子链.对应能够走到的点v对其进行统计,如果能够走到vY次,那么对应能够组成的可行方案就是C(Y,2)=Y*(Y-1)/2组。

过程维护一下即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int >mp[3005];int vis[3005];int v[3005];int ans[3005];void Dfs(int u,int from,int len){    if(len==2)    {        ans[u]++;    }    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        if(v==from)continue;        if(len+1<=2)        Dfs(v,u,len+1);    }}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)mp[i].clear();        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[x].push_back(y);        }        __int64 output=0;        for(int i=1;i<=n;i++)        {            memset(ans,0,sizeof(ans));            memset(vis,0,sizeof(vis));            Dfs(i,-1,0);            for(int j=1;j<=n;j++)            {                output+=(__int64)ans[j]*(__int64)(ans[j]-1)/2;            }        }        printf("%I64d\n",output);    }}






0 0
原创粉丝点击