【cf489】D. Unbearable Controversy of Being(暴力)
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.
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
【cf489】D. Unbearable Controversy of Being(暴力)的更多相关文章
- 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 489D Unbearable Controversy of Being (搜索)
Unbearable Controversy of Being 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/B Descrip ...
- CodeForces 489D Unbearable Controversy of Being (不知咋分类 思维题吧)
D. Unbearable Controversy of Being time limit per test 1 second memory limit per test 256 megabytes ...
- Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)
这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄 ...
- CodeForces 489D Unbearable Controversy of Being
题意: 给出一个n个节点m条边的有向图,求如图所示的菱形的个数. 这四个节点必须直接相邻,菱形之间不区分节点b.d的个数. 分析: 我们枚举每个a和c,然后求出所有满足a邻接t且t邻接c的节点的个数记 ...
- [CF489D]Unbearable Controversy of Being
题目大意:求有向图中这种图的数量 从分层图来考虑,这是一个层数为3的图 枚举第一个点能到达的所有点,对他们进行BFS求第三层的点(假装它是BFS其实直接枚举效果一样) 代码: #include< ...
- 【Codeforces 489D】Unbearable Controversy of Being
[链接] 我是链接,点我呀:) [题意] 让你找到(a,b,c,d)的个数 这4个点之间有4条边有向边 (a,b)(b,c) (a,d)(d,c) 即有两条从a到b的路径,且这两条路径分别经过b和d到 ...
- 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)-D
题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...
随机推荐
- Linux 监测磁盘常用的工具sar iostat vmstat
Linux 检测内存常用的工具sar iostat vmstat #每秒刷新一次显示2次 sar -d 1 2 iostat -kx 1 2 vmstat -d 1 2 磁盘统计信息解释 tps 每秒 ...
- Creating a Fragment: constructor vs newInstance()
from stack overflow and another chapter I recently grew tired of constantly having to know String ke ...
- datagrid("getSelections")只获取一行
页面加载方法如下 function loadSfXtjsList(sfXtjsListId, url, onClickFun) { $("#eastPanel").panel({ ...
- jQuery通过text值来设置选定,以及遍历select的选项个数和遍历
真是醉了,网上搜了很久,全都是千篇一律的. 大家都拷贝来拷贝去,全是错的. 通过text值来设置select选定 $("#CompanyID").find("option ...
- C#指南,重温基础,展望远方!(9)C#接口
接口定义了可由类和结构实现的协定. 接口可以包含方法.属性.事件和索引器. 接口不提供所定义的成员的实现代码,仅指定必须由实现接口的类或结构提供的成员. 接口可以采用多重继承. 在以下示例中,接口 I ...
- cocos2dx 关于lua 绑定的环境配置官方文档翻译与 将自己定义c++方法绑定到lua的的方法
网上有好多写如何讲自己定义的方法绑定到lua的文章,当中都仅仅对环境配置做了简单的介绍,看到有的帖子写在绑定中遇到了各种各样的error.大部分是因为环境配置不对导致的,下面是官方的文档有标准的说明, ...
- 记一次解决layui 的bug - layer.open 与 layui渲染问题
场景是这样的,通过layer打开一个弹窗,里面放置一个表单,表单是用layui来渲染的. 当弹窗完成之后,我需要渲染表单中的一些内容.譬如laydate. layer.open({ type: 1, ...
- Java 枚举(enum) 的常见用法和开发规范
JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static final ...
- AngularJS实现鼠标右键事件
常规javascript鼠标右键直接在标签上加contextmenu="alert('a')"即可,现在angular通过directive来定义一个右键指令. app.direc ...
- 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per t ...