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 ...
随机推荐
- Linux whereis 搜索命令位置
在学习 兄弟连 linux教学视频 的时候,我将所学的 linux 命令记录在我的博客中,方便自己查阅. 文件搜索命令: whereis 基础的命令 命令名称:whereis 命令的所在路径:/usr ...
- Python小爬虫,用Python3.X编写
import urllib.request # 导入urlib.request模块import re # 导入re模块 # 获得每一页的网址并返回def get_url(pageNumber): ne ...
- 201621123016《Java程序设计》第1周学习总结
1. 本周学习总结 本周的学习内容:java的发展历史,java程序设计环境,java简单语法. java与c++一样是一门面向对象的程序设计语言(相比于c++它是一门更彻底的面向对象的程序设计语言) ...
- [Xcode 实际操作]九、实用进阶-(32)项目的打包上传和提交审核以及下架处理
目录:[Swift]Xcode实际操作 本文将演示如何将一个应用程序进行打包上传,并提交审核以及下架处理. 点击项目[DemoApp]->[Build Settings]编译设置->[Pr ...
- 【Netty】利用Netty实现心跳检测和重连机制
一.前言 心跳机制是定时发送一个自定义的结构体(心跳包),让对方知道自己还活着,以确保连接的有效性的机制. 我们用到的很多框架都用到了心跳检测,比如服务注册到 Eureka Server 之后会维 ...
- JAVA团队开发手册 - 3. 开发流程
开发流程 对于一个项目,最大的问题就是如何拆解为任务,分配到合适的人手里,并在有限的时间内完成它. 就像做建筑工程一样,其实做IT也是可以量化的,可能有的人砌砖砌得慢一些,有的人快一些. 但是我们把整 ...
- python之文件路径截取 & endswith()
文件路径截取: >>> import os >>> path = '/etc/singfor/passwd/sunny/test.log' >>> ...
- jave 计算音视频时长
File source = new File("视频.mp4"); Encoder encoder = new Encoder(); try { MultimediaInfo in ...
- UG 常用设置
Q01:UG制图,添加基本视图之后的中心线怎么去掉? A01:“菜单-->文件-->首选项-->制图-->视图-->公共-->常规-->□带中心线创建”,取消 ...
- String的小笔记
String类的对象是不可变的! 在使用String类的时候要始终记着这个观念.一旦创建了String对象,它就不会改变. String类中也有可以改变String中字符串的方法,但只要是涉及改变的方 ...