Codeforces Round #277.5 (Div. 2)-D
题意:求该死的菱形数目。直接枚举两端的点。平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2.
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
typedef long long LL;
using namespace std; const int mod=1e9 +7;
const int maxn=3005;
const int maxm=30005; int first[maxn],nex[maxm],v[maxm],ecnt,g[maxn][maxn]; void add_(int a,int b)
{
v[ecnt]=b;
nex[ecnt]=first[a];
first[a]=ecnt++;
}
int main()
{
int n,m,x,y;
while(~scanf("%d%d",&n,&m))
{
memset(first,-1,sizeof first);ecnt=0;
memset(g,0,sizeof g);
for(int i=0;i<m;i++)
scanf("%d%d",&x,&y),add_(x,y),g[x][y]=1;
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(i!=j){
int r=0;
for(int e=first[i];~e;e=nex[e])
if(g[v[e]][j])r++;
ans+=r*(r-1)/2;
}
}
printf("%d\n",ans);
}
return 0;
}
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, c and 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
Codeforces Round #277.5 (Div. 2)-D的更多相关文章
- 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) --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)-D. Unbearable Controversy of Being
http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...
- 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 ...
- Codeforces Round #277.5 (Div. 2)-A. SwapSort
http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...
- 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)B——BerSU Ball
B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- 算法分析-leedcode正则题目
Implement regular expression matching with support for '.' and '*'. 首先这里有个可能大家不知道的地方: if p[0] = '*', ...
- 编写最简单的 iPhone 界面切换应用
编写最简单的 iPhone 界面切换应用 以下是在iOS中最简单的界面切换示例.使用了多个Controller,并演示Controller之间在切换界面时的代码处理. 实现的应用界面: 首先,创建 ...
- 全国计算机等级考试二级教程-C语言程序设计_第6章_字符型数据
#include <stdio.h> main() { char c; char d; c = ; d = '; if (c == d) { printf("yes\n" ...
- UVALive 3635 Pie 切糕大师 二分
题意:为每个小伙伴切糕,要求每个小盆友(包括你自己)分得的pie一样大,但是每个人只能分得一份pie,不能拿两份凑一起的. 做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又 ...
- eclipse建立cocos2d-x开发环境
前提: 已经安装了eclipse.能够正常开发 android应用 环境:windows 工具:1.已经集成了adt的eclipse,能够开发android应用.没有的,能够下载.下载地址:http: ...
- DQL
DQL(Data QueryLanguage) 基本格式 select * from 表名 对于列进行限制 格式一:取指定列 select 列1,列2 from 表名 格式二:为列起别名的三种表示法, ...
- bootstrap注意事项(七)图片
在本章中,我们将学习 Bootstrap 对图片的支持.Bootstrap 提供了三个可对图片应用简单样式的 class: .img-rounded:添加 border-radius:6px 来获得图 ...
- Deep Clone 常用方式总结
Deep Clone Example 总结 Deep Clone 一般有如下几种实现方式: 纯手工每个类实现赋值 (ps: 不做介绍,一般都不想这么玩) 序列化和反序列化 纯反射 emit 或 Exp ...
- RandomAccessFile浅析
RandomAccessFile类中的write方法有以下的注意事项: 首先write方法每次都写入一个字节 api中write方法如下 public void write(int b) throws ...
- MYSQL区分大小写
MYSQL区分大小写 1.linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写: 2.用root帐号登录后,在/etc/my.cnf 中的[mysqld]后添加添加lower ...