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 }

 

转载于:https://www.cnblogs.com/towerbird/p/11257902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值