Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being
http://codeforces.com/problemset/problem/489/D
1 second
256 megabytes
standard input
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 a, b, cand 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.
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 4
1 2
2 3
1 4
4 3
1
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
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
void solve(){
int i, j, k;
int ans = ;
int cnt[MAXN];
memset(map, , sizeof(map));
for(i = ; i < m; i++){
scanf("%d %d", &a, &b);
map[a][b] = ;
}
for(i = ; i <= n; i++){
memset(cnt, , sizeof(cnt));
for(j = ; j <= n; j++){
if(map[i][j] == ){
for(k = ; k <= n; k++){
if(i != k && map[j][k] == ){
cnt[k]++;
}
}
}
}
for(j = ; j <= n; j++){
if(cnt[j] > ){
ans += cnt[j] * (cnt[j] - ) / ;
}
}
}
printf("%d\n", ans);
}
int main(){
while(scanf("%d %d", &n, &m) != EOF){
solve();
}
return ;
44 }
Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being的更多相关文章
- Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)
这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄 ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. SwapSort time limit per test 1 seco ...
- Codeforces Round #277.5 (Div. 2)-D
题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...
- Codeforces Round #277.5 (Div. 2)部分题解
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)
http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...
- Codeforces Round #277.5 (Div. 2)-B. BerSU Ball
http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...
随机推荐
- ruby版本错误的解决方法
创建: 2018/09/18 完成: 2018/09/18 现在基本上不怎么开新博文了,目前着手的几个方面现有的博文已经全面覆盖.基本上都是更新原博,毕竟融合在一个页面里面方便自己工作使用. 遇到一些 ...
- 环形文字 + css3制作图形 + animation无限正反旋转的一个小demo
少啰嗦,先看效果图: (注意文字和太极图均可旋转,太极图使用css写成的!) css: /*太极图css--*/ .Taiji { margin: 100px; width: 192px; heigh ...
- CAS单点登录之服务端部署
一.CAS服务端搭建 1.1 CAS支持Http登录配置 CAS默认是要https的链接才能登录的,不过学习的话是可以先去掉https限制,本博客介绍的是基于Cas4.2.7的,之前改过4.0的,详情 ...
- chmod 详解
http://man.linuxde.net/chmod chmod u+x,g+w f01 //为文件f01设置自己可以执行,组员可以写入的权限 chmod u=rwx,g=rw,o=r f01 c ...
- MongoDB 删除数据
删除mongodb集合中的数据可以使用remove()函数.remove()函数可以接受一个查询文档作为可选参数来有选择性的删除符合条件的文档.remove()函数不会删除集合本身,同时,原有的索引也 ...
- 测试 | 单元测试工具 | JUnit | 参数化
被测试类: package project; public class MyCalendar2 { public int getNumberOfDaysInMonth(int year, int mo ...
- 统计最长回文串(传说中的Manacher算法)Hihocoder 1032
算法的核心就在这一句上了:p[i] = min(p[2*id-i], p[id] + id - i); #include <iostream> #include <cstdio> ...
- Django (九) 项目开发流程&项目架构
项目开发流程&项目架构 1. 软件开发的一般流程 1. 需求分析及确认: 由需求分析工程师与客户确认甚至挖掘需求.输出需求说明文档. 2. 概要设计及详细设计: 开发对需求进行概要设计,包 ...
- AKOJ-1695-找素数
题意: 给定区间L,R. 计算区间中素数个数. 2 <= L,R <= 2147483647, R-L <= 1000000. 思路: 素数区间筛 先筛(2-sqrt(r)). 再用 ...
- HDU6299(2018多校第一场)
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6299 两个字符串的排序可以分成四种情况: (1)str1左少右多 vs str2 左多右 ...