http://codeforces.com/problemset/problem/489/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条路,问你能组成几个如题目描述一样的菱形。 暴力i->j->k 的路径数目, 即从i到k,能有多少个j能够满足条件,之后求组合数C(2, n)即可

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 #define MAXN 3030
 6 
 7 int map[MAXN][MAXN];
 8 int n, m, a, b;
 9 
 void solve(){
     int i, j, k;
     int ans = ;
     int cnt[MAXN];
     memset(map, , sizeof(map));
     for(i = ; i < m; i++){
         scanf("%d %d", &a, &b);
         map[a][b] = ;
     }
     for(i = ; i <= n; i++){
         memset(cnt, , sizeof(cnt));
         for(j = ; j <= n; j++){
             if(map[i][j] == ){
                 for(k = ; k <= n; k++){
                     if(i != k && map[j][k] == ){
                         cnt[k]++;
                     }
                 }
             }
         }
         for(j = ; j <= n; j++){
             if(cnt[j] > ){
                 ans += cnt[j] * (cnt[j] - ) / ;
             }
         }
     }
     printf("%d\n", ans);
 }
 
 int main(){
     while(scanf("%d %d", &n, &m) != EOF){
         solve();
     }
     return ;

44 }

Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being的更多相关文章

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

    这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄 ...

  2. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  3. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  4. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  5. Codeforces Round #277.5 (Div. 2)-D

    题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...

  6. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  7. Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)

    http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...

  8. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  9. Codeforces Round #277.5 (Div. 2)-B. BerSU Ball

    http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...

随机推荐

  1. 201621123016 《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  2. 动态加载dll

    extern "C" MMUPDATENOTIFY_IMPEXP bool _cdecl NotifyThrift(char* chThriftIp, char* chPor) H ...

  3. C#中的yield return

    4.1 迭代器块 一个迭代器块(iterator block)是一个能够产生有序的值序列的块.迭代器块和普通语句块的区别就是其中出现的一个或多个yield语句. yield return语句产生迭代的 ...

  4. [Xcode 实际操作]二、视图与手势-(8)UIView视图的纹理填充

    目录:[Swift]Xcode实际操作 本文将演示将导入的图片作为纹理,平铺整个屏幕. 往项目中导入一张图片. 点击底部左下角的图标->[Import]->选择需要导入的图片->[O ...

  5. [Xcode 实际操作]九、实用进阶-(31)为IAP(支付方式)内购功能的具体实现和测试

    目录:[Swift]Xcode实际操作 本文将演示如何为IAP(支付方式)内购功能的具体实现和测试. 内购是苹果市场上的一种常见的盈利方式. 在项目中确保已经安装了第三方库[Pod],双击[Podfi ...

  6. String字符串操作题

    /** * 反转键盘录入字符串 * 反转键盘录入的字符串 * 反转键盘录入的字符串 * 反转键盘录入的字符串 * */ Scanner sc = new Scanner(System.in);Stri ...

  7. [題解](最小生成樹)luogu_P1265

    首先考虑最小生成树的模型,唯一不同的是第二种情形. 即“三个或三个以上的城市申请修建的公路成环” 考虑该情形,因为修路的申请是申请离它最近的城市,所以上述条件实质上为 “存在三个或三个以上的城市,他们 ...

  8. Django (二) url 和 模板

    1. URL URL地址说明: 使用url给视图函数传参数 在url配置中将正则部分小括号括起来.比如: url(r'^time/plus/(\d{1,2})/$', views.hours_ahea ...

  9. HDU4035(概率期望、树形、数学)

    和ZOJ3329有些像,都是用期望列出来式子以后,为了解式子,设A[i],B[i],此题又多了C[i],然后用递推(此题是树形dp)去求得ABC,最后结果只跟ABC有关,跟列写的期望数组根本无关. 虽 ...

  10. datagridview连接数据库的简单实现

    对于需要从数据库获取数据的列,在DataPropertyName填写对应的列名字. 在Load函数中添加如下代码,先关闭自动生成列选项,然后得到一个查询的datatable,并将其绑定到datasou ...