CodeForces 489D Unbearable Controversy of Being (搜索)
Unbearable Controversy of Being
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121332#problem/B
Description
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 Input
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
题意:
给出一个有向图;求总共出现多少组菱形子图(如题);
菱形图:即(a, b), (b, c), (a, d), (d, c) 均直接联通;
题解:
n的规模为3000;
可以直接对每个点跑一遍深度为2的dfs.
每次dfs记录有多少条长度为2的边,C_n^m 累加即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 3100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n,m;
bool c[maxn][maxn];
vector<int> g[maxn];
int ans[maxn];
void dfs(int s, int len, int fa) {
if(len == 2) {
ans[s]++;
return;
}
int sz = g[s].size();
for(int i=0; i<sz; i++) {
if(g[s][i] == fa) continue;
dfs(g[s][i], len+1, s);
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d %d", &n, &m) != EOF)
{
memset(c, 0, sizeof(c));
for(int i=0; i<maxn; i++) g[i].clear();
for(int i=1; i<=m; i++) {
int x,y; scanf("%d %d", &x, &y);
c[x][y] = 1;
g[x].push_back(y);
}
LL Ans = 0;
for(int i=1; i<=n; i++) {
memset(ans, 0, sizeof(ans));
dfs(i, 0, i);
for(int j=1; j<=n; j++) {
if(ans[j] < 2) continue;
else Ans += (LL)ans[j]*(LL)(ans[j]-1)/2LL;
}
}
printf("%I64d\n", Ans);
}
return 0;
}
CodeForces 489D Unbearable Controversy of Being (搜索)的更多相关文章
- CodeForces 489D Unbearable Controversy of Being (不知咋分类 思维题吧)
D. Unbearable Controversy of Being time limit per test 1 second memory limit per test 256 megabytes ...
- CodeForces 489D Unbearable Controversy of Being
题意: 给出一个n个节点m条边的有向图,求如图所示的菱形的个数. 这四个节点必须直接相邻,菱形之间不区分节点b.d的个数. 分析: 我们枚举每个a和c,然后求出所有满足a邻接t且t邻接c的节点的个数记 ...
- 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
[链接] 我是链接,点我呀:) [题意] 让你找到(a,b,c,d)的个数 这4个点之间有4条边有向边 (a,b)(b,c) (a,d)(d,c) 即有两条从a到b的路径,且这两条路径分别经过b和d到 ...
- Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)
这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄 ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- Codeforces 782C. Andryusha and Colored Balloons 搜索
C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...
- Codeforces Gym101246J:Buoys(三分搜索)
http://codeforces.com/gym/101246/problem/J 题意:给出n个点坐标,要使这些点间距相同的话,就要移动这些点,问最少的需要的移动距离是多少,并输出移动后的坐标. ...
随机推荐
- Java中JNI的使用详解第二篇:JNIEnv类型和jobject类型的解释
上一篇说的是一个简单的应用,说明JNI是怎么工作的,这一篇主要来说一下,那个本地方法sayHello的参数的说明,以及其中方法的使用 首先来看一下C++中的sayHello方法的实现: JNIEXPO ...
- 点(Dot)与像素(Pixel)的区别
DPI中的点(Dot)与图像分辨率中的像素(Pixel)是容易混淆的两个概念, DPI中的点可以说是硬件设备最小的显示单元, 而像素则既可是一个点,又可是多个点的集合.在扫描仪扫描图像时,扫描仪的每一 ...
- Android中常见的MVC模式
MVC模式的简要介绍 MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller). MVC模式的目的就是实现Web系统的职能分工. Model层实现系统中的业务 ...
- HDU 4923
题目大意: 给出一串序列Ai{0,1},求一个序列Bi[0,1](Bi<Bi+1),使得sigama(Ai-Bi)^2最小 思路: 若B相同,则取A的平均数可使方差最小 若B有序, 若A== ...
- Smack IQ包的扩展
前几天一直很烦躁,怎么扩展smack的IQ包堵了我好久,今天静下心来看了下smack的源码,把这个问题解决了.下面给出步骤: 如果我们要扩展一个如下所示的IQ包: <iq id="00 ...
- BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1627 网格图,给出起点,终点,障碍,求最短路. 分析 简单的宽搜. #include < ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法。。
[原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法. 1.单个NAL包单元 12字节 ...
- JavaScript在IE和Firefox(火狐)的不兼容问题解决方法小结 【转】http://blog.csdn.net/uniqer/article/details/7789104
1.兼容firefox的 outerHTML,FF中没有outerHtml的方法. 代码如下: if (window.HTMLElement) { HTMLElement.prototype.__de ...
- .NET面试题系列
索引: .NET框架基础知识[1] - http://www.cnblogs.com/haoyifei/p/5643689.html .NET框架基础知识[2] - http://www.cnblog ...