http://codeforces.com/contest/489/problem/D

很显然,我们只需要找对于每个点能到达的深度为3的点的路径的数量,那么对于一个深度为3的点,如果有a种方式到达,那么有方案数(a-1+1)*(a-1)/2

可是我用dfs找路径就tle了QAQ

于是orz别人的代码,,,,是暴力。。。。。。。。。。。。。。。。。。。。。。。。直接两重循环orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
const int N=3005;
struct dat { int to, next; }e[N*10];
int cnt, vis[N], c[N], n, m, ihead[N];
void add(int u, int v) { e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; }
void bfs(int x, int dep) {
rdm(x, i) {
int y=e[i].to;
rdm(y, j) {
int z=e[j].to;
if(x==z) continue;
++c[z];
}
}
}
ll ans;
int main() {
read(n); read(m);
for1(i, 1, m) { int u=getint(), v=getint(); add(u, v); }
for1(i, 1, n) {
for1(j, 1, n) vis[j]=0, c[j]=0;
bfs(i, 1);
//for1(j, 1, n) cout << c[j] << ' '; cout << endl;
for1(j, 1, n) if(c[j]>=2) {
--c[j];
ans+=(ll)(c[j]+1)*c[j]/2;
}
}
printf("%I64d\n", ans);
return 0;
}

  


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.

Input

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.

Output

Print the required number of "damn rhombi".

Sample test(s)
input
5 4
1 2
2 3
1 4
4 3
output
1
input
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
output
12

【cf489】D. Unbearable Controversy of Being(暴力)的更多相关文章

  1. 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 ...

  2. CodeForces 489D Unbearable Controversy of Being (搜索)

    Unbearable Controversy of Being 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/B Descrip ...

  3. CodeForces 489D Unbearable Controversy of Being (不知咋分类 思维题吧)

    D. Unbearable Controversy of Being time limit per test 1 second memory limit per test 256 megabytes ...

  4. Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)

    这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄 ...

  5. CodeForces 489D Unbearable Controversy of Being

    题意: 给出一个n个节点m条边的有向图,求如图所示的菱形的个数. 这四个节点必须直接相邻,菱形之间不区分节点b.d的个数. 分析: 我们枚举每个a和c,然后求出所有满足a邻接t且t邻接c的节点的个数记 ...

  6. [CF489D]Unbearable Controversy of Being

    题目大意:求有向图中这种图的数量 从分层图来考虑,这是一个层数为3的图 枚举第一个点能到达的所有点,对他们进行BFS求第三层的点(假装它是BFS其实直接枚举效果一样) 代码: #include< ...

  7. 【Codeforces 489D】Unbearable Controversy of Being

    [链接] 我是链接,点我呀:) [题意] 让你找到(a,b,c,d)的个数 这4个点之间有4条边有向边 (a,b)(b,c) (a,d)(d,c) 即有两条从a到b的路径,且这两条路径分别经过b和d到 ...

  8. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  9. Codeforces Round #277.5 (Div. 2)-D

    题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...

随机推荐

  1. AsyncTask doinbackground onProgressUpdate onCancelled onPostExecute的基本使用

    对于异步操作的原理我就不讲了.在这我着重讲怎么使用异步操作的doinbackground onProgressUpdate onCancelled onPostExecute这四个方法 doinbac ...

  2. ios开发之-继承的实现运用

    // // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...

  3. 将图片转成base64字符串并在JSP页面显示的Java代码

    *本事例主要讲了如下几点:  * 1:将图片转换为BASE64加密字符串.  * 2:将图片流转换为BASE64加密字符串.  * 3:将BASE64加密字符串转换为图片. * 4:在jsp文件中以引 ...

  4. activeMQ Jms Demo

    概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经 ...

  5. ubuntu sudo 和su的区别

    ubuntu用户管理 sudo意思就是super-user do,让当前用户暂时以管理员的身份root来执行这条命令. su是用来改变当前用户的,su root,就是将当前用户切换为root,用了su ...

  6. 编码gbk ajax的提交

    客户端

  7. Android中TextView内容过长加省略号

          textview中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中 Android:ellipsize = "end"   省略号在结尾 and ...

  8. MFC显示GIF动画图片

    本帖则将讨论如何在MFC的对话框里显示GIF动画图片.一些关于传统控件的美化方法正在研究当中会陆续发帖的. 这是本帖用到的一个VS2008例程.  附件  GifPicture.rar (138.1 ...

  9. 关闭windows打印服务

    1.关闭打印服务:开始-运行-services.msc或打开控制面板-管理工具-服务,打开服务列表,找到Print Spooler(打印服务),关闭(右击,点“关闭”).2.删除打印缓存:进入c:\\ ...

  10. Linux 用 shell 脚本 批量 导入 csv 文件 到 mysql 数据库

    前提: 每个csv文件第一行为字段名 创建的数据库字段名同csv 文件的字段名 1. 批量导入 多个 csv 文件 for file in ./*.csv;do mv $file tablename. ...