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. man帮助的使用

    本篇博文转自Linux社区,谨在此记录使用: ——————Linux系统man帮助的使用技巧———————— man的查询后面的数字含义:1用户在shell换机中可以操作的指令或可执行文件2系统核心可 ...

  2. MySQL基础知识(一)-超详细MySQL安装教程

    简介 原计划,今天这篇想要给小伙伴们讲解一下python操作mysql数据库,但是由于近期换了一台新的电脑,所以一看mysql数据库都没安装,所有才有了这篇文章.尽管网上不乏此类型的文章,但是刚好自己 ...

  3. 五粮液【线段树】By cellur925

    题目传送门 考场上感觉的确是线段树,还要维护区间最值...最值怎么维护?还要区间修改?\(update\)的时候加一下就好了吧...之后怎么搞啊?\(qwqwq\)之后好像不太会了...果断删除几乎快 ...

  4. 解决tomcat一闪而过(转)

    转自 http://blog.csdn.net/znn626/article/details/7893555 遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配 ...

  5. salt命令

    salt-key -L list在master上所有收到的公钥连接请求 -A accept所有pending的请求. -D 删除所有 在minion上启动服务后,几十秒后会在/etc/salt/pki ...

  6. C# 面向对象之继承后初始化顺序

    使用继承之后当我们初始化一个子类时子类的初始化顺序为: (1)初始化类的实例字段 (2)调用基类的构造函数,如果没有指明基类则调用System.Object的构造函数; (3)调用子类的构造函数

  7. Tinghua Data Mining 6

    Networks 多层感知机 不是说这个神经网络要与人的大脑神经完全相似,也不是说要多么的强大,而是在一定程度上模拟了人脑神经元的能力,就足够了 为什么要w0呢,因为没有w0超平面一定会经过原点,所以 ...

  8. linux下输出json字符串,用python格式化

    echo '{"name":"chen","age":"11"}' |python -m json.tool 如果是文件 ...

  9. KEIL MDK之RTX的移植

    原文: http://lib.csdn.net/article/embeddeddevelopment/12240 一 将MDK安装目录的C:\keil\ARM\RL\RTX\Config下面的配置文 ...

  10. jQuery val()方法及valHooks源码解读

    val: function( value ) { var hooks, ret, isFunction, elem = this[0]; if ( !arguments.length ) {//无参数 ...