D. Unbearable Controversy of Being

 

 http://codeforces.com/contest/489/problem/D

 

 

暴力,枚举所有"damn rhombus"

 

对于任意a、c,设其a->i->c的路个数为cnt,其"damn rhombus"个数为cnt*(cnt-1)/2

 

初始化cnt[][],然后遍历所有点即可

 

 

 1 public class Main {
 2     public static void main(String[] args) {
 3         Scanner io = new Scanner(System.in);
 4         int n = io.nextInt(), m = io.nextInt();
 5         //链表存图,节约时间
 6         int[][] g = new int[3010][3010];
 7         int[][] cnt = new int[3010][3010];
 8 
 9         for (int i = 0; i < m; i++) {
10             int a = io.nextInt(), b = io.nextInt();
11             g[a][++g[a][0]] = b;
12         }
13         //初始化cnt,避免在两遍n循环中重复计算,节约时间
14         for (int i=1;i<=n;i++)for (int j=1;j<=g[i][0];j++){
15             int c=g[i][j];
16             for (int k=1;k<=g[c][0];k++)cnt[i][g[c][k]]++;
17         }
18 
19         int ans = 0;
20         for (int a = 1; a <= n; a++) {
21             for (int c = 1; c <= n; c++) {
22                 if (c == a) continue;
23                 ans += cnt[a][c] * (cnt[a][c] - 1) / 2;
24             }
25         }
26         System.out.println(ans);
27     }
28 }

 

posted @ 2019-07-28 10:03  dodoBehind  阅读(123)  评论(0编辑  收藏  举报