zoukankan      html  css  js  c++  java
  • Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being

    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 
    10 void solve(){
    11     int i, j, k;
    12     int ans = 0;
    13     int cnt[MAXN];
    14     memset(map, 0sizeof(map));
    15     for(i = 0; i < m; i++){
    16         scanf("%d %d", &a, &b);
    17         map[a][b] = 1;
    18     }
    19     for(i = 1; i <= n; i++){
    20         memset(cnt, 0sizeof(cnt));
    21         for(j = 1; j <= n; j++){
    22             if(map[i][j] == 1){
    23                 for(k = 1; k <= n; k++){
    24                     if(i != k && map[j][k] == 1){
    25                         cnt[k]++;
    26                     }
    27                 }
    28             }
    29         }
    30         for(j = 1; j <= n; j++){
    31             if(cnt[j] > 1){
    32                 ans += cnt[j] * (cnt[j] - 1) / 2;
    33             }
    34         }
    35     }
    36     printf("%d ", ans);
    37 }
    38 
    39 int main(){
    40     while(scanf("%d %d", &n, &m) != EOF){
    41         solve();
    42     }
    43     return 0;

    44 } 

  • 相关阅读:
    Python数据类型之数值-Python基础前传(5)
    R语言之数据可视化
    R语言之数据可视化
    R语言基础
    R语言基础
    R语言基础
    R语言入门
    R语言入门
    用 python 爬取 gutenberg 上的英文科幻小说
    Python 在数据科学中的应用
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4113855.html
Copyright © 2011-2022 走看看