CodeForces 489D Unbearable Controversy of Being (不知咋分类 思维题吧)
1 second
256 megabytes
standard input
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, cand 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 题意: 就是找有多少个四点组合符合图中那样的。 思路: 注意到一点,虽然n比较大,有3000个,在满载情况下m的数量可以达到3000*2999=9000000,但是m是比较小的只有30000。
所以完全可以遍历一遍边而花费代价不高。
想着想着就想到一种方法。。不算什么算法。。也不难实现的
用一个结构体edge保存每条边的起点和终点,再用一个邻接矩阵保存某个点到某个点是否有边连通。
再用一个数组 c[x][y] 保存 有多少种方法,是 x 经过一个点再到 y的,这样子以x和y为端点的菱形的数量就是 c[x][y]*(c[x][y]-1)/2 了
遍历一次边集,对于每条边,起点终点分别为x和y,然后遍历一下z,看z是否与x相连,相连的话,则c[z][y]++,这一步需要o(m*n)
统计完以后遍历一次c矩阵,用上面的公式操作矩阵之和就是答案了,这一步需要o(n*n)
总复杂度大概在10^8左右,ok
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
const int M = ;
int cas = ; struct _edge{
int u,v;
}edge[M]; bool g[N][N];
int c[N][N];
int n,m; void run()
{
memset(g,,sizeof(g));
memset(c,,sizeof(c));
for(int i=;i<m;i++)
{
scanf("%d%d",&edge[i].u,&edge[i].v);
g[edge[i].u][edge[i].v]=;
}
for(int i=;i<m;i++)
{
int &x=edge[i].u;
int &y=edge[i].v;
for(int z=;z<=n;z++)
if(z!=x && z!=y && g[z][x])
c[z][y]++;
}
int ans = ;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j && c[i][j])
ans+=c[i][j]*(c[i][j]-);
printf("%d\n",ans/);
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
while(scanf("%d%d",&n,&m)!=EOF)
run();
return ;
}
CodeForces 489D Unbearable Controversy of Being (不知咋分类 思维题吧)的更多相关文章
- 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
题意: 给出一个n个节点m条边的有向图,求如图所示的菱形的个数. 这四个节点必须直接相邻,菱形之间不区分节点b.d的个数. 分析: 我们枚举每个a和c,然后求出所有满足a邻接t且t邻接c的节点的个数记 ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- codeforces round 472(DIV2)D Riverside Curio题解(思维题)
题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...
- C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题
C. Meaningless Operations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #280 (Div. 2) E. Vanya and Field 思维题
E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
随机推荐
- [原创]java WEB学习笔记43:jstl 介绍,core库详解:表达式操作,流程控制,迭代操作,url操作
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- [原创] hadoop学习笔记:重新格式化HDFS文件系统
所谓的重新格式化HDFS文件系统,实际意味着重新的创建一个HDFS文件系统.也就是说,必须将先前的已经有的文件系统配置删除.如下: 笔者采用的是最小化安装 这个是core-site.xml配置 这个是 ...
- vo类总结
1.Camera类 camera类里面,首先camera有5个变量,fx_,fy_,cx_,cy_,depth_scale_5个变量,由外部传fx,fy,cx,cy,depth_scale给它.定义了 ...
- WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...
- kreuz-frankfurt-sample-generic-2019-02-08.xodr文件解读
第1行:xml语法所遵循的版本. L2:文件封装标记. L3:Opendrive的主要修订编号 次要修订编号 供应商. L4:记录有关地理参考坐标系的参数,投影-横轴墨卡托 a-地球椭球长半 ...
- 给手机发验证码 综合使用 (忘记密码处理 php发验证码 重置用户密码)
前台页面 提取手机号调用 jQuery的ajax,到发送验证码 [php] view plain copy <title>找回密码 - 2015年xxx报名系统</title> ...
- Selenium-元素定位与操作
UI的自动化本质就是识别元素,操作元素,而元素的识别就是通过HTML的标签和属性,所以对于基本的HTML的只是是必备的 随着页面复杂度的提升,加之很多公司的开发也没有统一规范,这就给识别元素造成了非常 ...
- Javascript-- jQuery样式篇(二)
jQuery的属性与样式 .attr()与.removeAttr() 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.如:在img元素中,src就是元素的特性,用来标 ...
- linux命令学习笔记(38):cal 命令
cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历. “阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”. .命令格式: cal ...
- eclipse导入java web项目,项目出现红叉而其他地方没有红叉的问题解决方法
eclipse导入别人的Java web项目时会出现这种情况:仅项目名出现红叉而其他地方没有红叉的问题.这可能是以下几种情况导致的,其解决方法如下: 1.导入项目之前,请确认工作空间编码已设置为utf ...