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. 使用vs2019进行Linux远程开发

    通常,当我们开发Linux程序时有两种方案: 在Linux上直接编写程序并进行运行测试和调试 在Windows或Mac OS X上借助工具进行远程开发 虽然我自己是在Linux环境上直接进行开发的,但 ...

  2. lightoj1010【LCS】

    题意: 求最长公共子序列,并且这个子序列是字典序最小. 思路: LCS附一个值,dp[i][j].first代表以i,j的LCS的长度,dp[i][j].second代表LCS结尾元素,然后利用路径输 ...

  3. 3DMAX 烘培技术

    烘培是指,把光照信息渲染成贴图,而后把这个烘培后的贴图再贴回到场景中去的技术.烘培技术把光照计算的结果提前写入到了贴图中,因此在实时渲染中不需要进行耗时的光照计算,大大提高了实时渲染的效率. 烘培和渲 ...

  4. 洛谷P3857 [TJOI2008]彩灯(线性基)

    传送门 线性基裸题 直接把所有的状态都带进去建一个线性基 然后答案就是$2^{cnt}$($cnt$代表线性基里数的个数) //minamoto #include<cstdio> #inc ...

  5. vue2.0.js基础开发使用心得(结合实际项目对数据的增删改查)

    1.首先申明,没有使用vue 的组件,以及脚手架等,都是一些基础语法的使用. ------------------------------------------------------------- ...

  6. 【NOIP2015】 Day2 T2 字串 (多维动归)

    2018-09-12 原题传送门(洛谷)https://www.luogu.org/problemnew/show/P2679 模拟考试的时候完全没有想到 正确的DP方程呢 本来写了一个大致是对的转移 ...

  7. STM32 精确输出PWM脉冲数控制电机(转)

    STM32 精确输出PWM脉冲数控制电机 发脉冲两种目的1)速度控制2)位置控制 速度控制目的和模拟量一样,没有什么需要关注的地方发送脉冲方式为PWM,速率稳定而且资源占用少 stm32位置控制需要获 ...

  8. 给mysql默认root用户设置密码

    情况1:如果忘记了 MySQL 的 root 密码,可以用以下方法重新设置:   情况2:添加了phpmyadmin,登录时使用mysql的用户密码登录,但phpmyadmin不能使用空密码登录,即为 ...

  9. C# string.Compare()

    tring.Compare方法,用来比较2个字符串值得大小 string.Compare(str1, str2, true); 返回值: 1 : str1大于str2 0 : str1等于str2 - ...

  10. js_jquery

    引用 jQuery 是一个 JavaScript 库,不需要安装,直接引用就行 <!-- jQuery --> <script src="/static/vendors/j ...