zoukankan      html  css  js  c++  java
  • Codeforces489D——暴力水题——Unbearable Controversy of Being

    Description

    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 abc andd, 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 Input

    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
    /*
       复杂度为O(n*n) 不会爆
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAX = 3000 + 10;
    
    int map1[MAX][MAX];
    int map2[MAX][MAX];
    
    int main()
    {
        int n, m;
        int x, y;
        while(~scanf("%d%d", &n, &m)){
            memset(map1, 0, sizeof(map1));
            memset(map2, 0, sizeof(map2));
            for(int i = 1; i <= m ; i++){
                scanf("%d%d", &x, &y);
                map1[x][y] = 1;
            }
            for(int i = 1; i <= n ; i++){
                for(int j = 1; j <= n ;j++){
                    if(map1[i][j]){
                        for(int k = 1; k <= n; k++){
                            if(i != k && map1[j][k]){
                                map2[i][k]++;
                            }
                        }
                    }
                }
            }
            int sum = 0;
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= n ; j++){
                    if(map2[i][j] > 0)
                    sum += map2[i][j] * (map2[i][j] - 1) /2;
                }
            }
            printf("%d
    ", sum);
            }
        return 0;
    }
    

      

  • 相关阅读:
    MiniUI表单验证实践
    MiniUI官方表单验证示例
    MiniUI表单验证总结
    Js-事件分发与DOM事件流
    Windows远程桌面连接的利器-mRemote
    Git 以分支的方式同时管理多个项目
    GIT 如何合并另一个远程Git仓库的文件到本地仓库里某个指定子文件夹并不丢失远程提交记录?
    如何导入另一个 Git库到现有的Git库并保留提交记录
    Total Commander如何设置自定义快捷键在当前目录打开ConEmu
    PHP ECSHOP中 诡异的问题:expects parameter 1 to be double
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4667382.html
Copyright © 2011-2022 走看看