cf489D. Unbearable Controversy of Being
来源:互联网 发布:java 开源 paas 平台 编辑:程序博客网 时间:2024/04/23 22:34
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 a, b, c 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.
The first line of the input contains a pair of integers n, m (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.
Print the required number of "damn rhombi".
5 41 22 31 44 3
1
4 121 21 31 42 12 32 43 13 23 44 14 24 3
12
原本以为会超时的不敢交最后果断交了一下竟然AC了
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<list>#include<vector>using namespace std;const int maxn=3010;vector<int>vec1[maxn];vector<int>vec2[maxn];struct Node{int f,r;int visf[maxn],visr[maxn];}A[maxn];int dp[maxn][maxn];int main(){int n,m,i,j,k;scanf("%d%d",&n,&m);int a,b;for(i=1;i<=m;++i){scanf("%d%d",&a,&b);vec1[b].push_back(a);vec2[a].push_back(b);}for(i=1;i<=n;++i){for(j=0;j<vec1[i].size();++j){for(k=0;k<vec2[i].size();++k){dp[vec1[i][j]][vec2[i][k]]++;}}}long long ans=0;for(i=1;i<=n;++i){for(j=1;j<=n;++j){if(i==j)continue;ans=ans+dp[i][j]*(dp[i][j]-1)/2;}}printf("%lld\n",ans);return 0;}
- cf489D. Unbearable Controversy of Being
- Unbearable Controversy of Being
- Unbearable Controversy of Being CodeForces
- 【Codeforces】 489D Unbearable Controversy of Being
- CodeForces 489D Unbearable Controversy of Being
- CodeForces 489D. Unbearable Controversy of Being
- CodeForces 489D Unbearable Controversy of Being
- Codeforces 489D Unbearable Controversy of Being
- Codeforces 489D Unbearable Controversy of Being
- Codeforces 469D Unbearable Controversy of Being
- CodeForces 489D Unbearable Controversy of Being
- Codeforces Round #277.5 (Div. 2) D. Unbearable Controversy of Being
- codeforces 489d Unbearable Controversy of Being 暴力
- codeforces 489D Unbearable Controversy of Being (暴力乱搞)
- CF 489D Unbearable Controversy of Being BFS
- CF Unbearable Controversy of Being 489D 暴力
- Codeforces Round #277.5 (Div. 2) D——Unbearable Controversy of Being
- Codeforces 498D Unbearable Controversy of Being(暴力求菱形个数)
- 嵌入式系统开发常用网址
- 异常
- web工程中通过类加载器读取配置文件
- PAT-B 1010. 一元多项式求导
- 广度 / 深度优先搜索
- cf489D. Unbearable Controversy of Being
- C游戏 简单迷宫游戏开发
- 继承与多态的理解
- 计算机网络通过ip来知道对方的主机名
- JAVA SE 8 学习笔记(一)lambda表达式
- java注解
- java异常体系2
- 第四次C++作业
- thinkphp等框架开发中容易忽略的xss攻击