Codeforces Round #277.5 (Div. 2) D??Unbearable Controversy of Being_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:53:48
原创
1271人浏览过

D. Unbearable Controversy of Being

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

立即学习前端免费学习笔记(深入)”;

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 41 22 31 44 3
登录后复制

Output

Input

4 121 21 31 42 12 32 43 13 23 44 14 24 3
登录后复制

Output

12
登录后复制

水题,只要计算出每个点走两步以后到达第三个点的所有走法,然后C(2, n)


#include <map>#include <set>#include <list>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 3010;const int M = 30010;struct node{	int next;	int to;}edge[M];int head[N];int tot;int num[N][N];void addedge(int from, int to){	edge[tot].to = to;	edge[tot].next = head[from];	head[from] = tot++;}int main(){	int n, m;	while (~scanf("%d%d", &n, &m))	{		int u, v, w;		memset (head, -1, sizeof(head));		memset (num, 0, sizeof(num));		tot = 0;		for (int i = 1; i <= m; ++i)		{			scanf("%d%d", &u, &v);			addedge(u, v);		}		for (u = 1; u <= n; ++u)		{			for (int i = head[u]; ~i; i = edge[i].next)			{				v = edge[i].to;				for (int j = head[v]; ~j; j = edge[j].next)				{					w = edge[j].to;					num[u][w]++;				}			}		}		__int64 ans = 0;		for (int i = 1; i <= n; ++i)		{			for (int j = 1; j <= n; ++j)			{				if (i == j)				{					continue;				}				ans += (__int64)(num[i][j] - 1) * num[i][j] / 2;			}		}		printf("%I64d\n", ans);	}	return 0;}
登录后复制


HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号