Codeforces Round #277.5(Div. 2) D. Unbearable Controversy of Being【暴力】
来源:互联网 发布:怎么做数据分析 编辑:程序博客网 时间:2024/05/21 07:06
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 intersectionsa, b,c and d, such that there are two paths froma to c — one throughb 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 andm 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 andd 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. Nextm lines list the roads, one per line. Each of the roads is given by a pair of integersai, 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 41 22 31 44 3
1
4 121 21 31 42 12 32 43 13 23 44 14 24 3
12
题目大意:
找到四个点,形状和题中图一模一样.问一共能够组成多少组这样的4个点。
思路:
1、一开始想的是O(m^2)枚举两条边,然后判断四个点之间的边的形状是否和要求一样即可。
但是因为m比较大,还是TLE了.(CF冲1e9也是可以尝试的);
2、其实因为N比较小,我们直接O(n)枚举起点,然后暴力深搜长度为2的子链.对应能够走到的点v对其进行统计,如果能够走到vY次,那么对应能够组成的可行方案就是C(Y,2)=Y*(Y-1)/2组。
过程维护一下即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<vector>using namespace std;vector<int >mp[3005];int vis[3005];int v[3005];int ans[3005];void Dfs(int u,int from,int len){ if(len==2) { ans[u]++; } for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(v==from)continue; if(len+1<=2) Dfs(v,u,len+1); }}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++)mp[i].clear(); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); mp[x].push_back(y); } __int64 output=0; for(int i=1;i<=n;i++) { memset(ans,0,sizeof(ans)); memset(vis,0,sizeof(vis)); Dfs(i,-1,0); for(int j=1;j<=n;j++) { output+=(__int64)ans[j]*(__int64)(ans[j]-1)/2; } } printf("%I64d\n",output); }}
- Codeforces Round #277.5 (Div. 2) D. Unbearable Controversy of Being
- Codeforces Round #277.5(Div. 2) D. Unbearable Controversy of Being【暴力】
- Codeforces Round #277.5 (Div. 2) D——Unbearable Controversy of Being
- Codeforces Round #277.5 (Div. 2) D - Unbearable Controversy of
- codeforces 489d Unbearable Controversy of Being 暴力
- codeforces 489D Unbearable Controversy of Being (暴力乱搞)
- 【Codeforces】 489D Unbearable Controversy of Being
- CodeForces 489D Unbearable Controversy of Being
- CodeForces 489D. Unbearable Controversy of Being
- CodeForces 489D Unbearable Controversy of Being
- Codeforces 489D Unbearable Controversy of Being
- Codeforces 489D Unbearable Controversy of Being
- Codeforces 469D Unbearable Controversy of Being
- CodeForces 489D Unbearable Controversy of Being
- Unbearable Controversy of Being CodeForces
- CF Unbearable Controversy of Being 489D 暴力
- Codeforces 498D Unbearable Controversy of Being(暴力求菱形个数)
- Unbearable Controversy of Being
- [牛客]插入排序练习题
- PAT 1015. 德才论
- Js_面向对象_canvas_03
- (c语言)链表方法:13个人一次报号,报道3的人退出,输出最后一个人
- 功率放大器的温补电路
- Codeforces Round #277.5(Div. 2) D. Unbearable Controversy of Being【暴力】
- 每日刷题:lightoj 1005
- c#窗体小白知识(1)
- 我的sublime插件--随时更新
- 值类型和引用类型区别
- 机器学习练习三:逻辑回归
- nodejs mysql 实现分页
- unit2.1 自动安装虚拟机
- 关于乱码