Codeforces Round #277.5 (Div. 2)-D「建议收藏」

Codeforces Round #277.5 (Div. 2)-D

大家好,又见面了,我是全栈君。

题意:求该死的菱形数目。直接枚举两端的点。平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2.

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
typedef long long LL;
using namespace std;

const int mod=1e9 +7;
const int maxn=3005;
const int maxm=30005;

int first[maxn],nex[maxm],v[maxm],ecnt,g[maxn][maxn];

void add_(int a,int b)
{
    v[ecnt]=b;
    nex[ecnt]=first[a];
    first[a]=ecnt++;
}
int main()
{
    int n,m,x,y;
    while(~scanf("%d%d",&n,&m))
    {
        memset(first,-1,sizeof first);ecnt=0;
        memset(g,0,sizeof g);
        for(int i=0;i<m;i++)
            scanf("%d%d",&x,&y),add_(x,y),g[x][y]=1;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
           for(int j=1;j<=n;j++)
           if(i!=j){
               int r=0;
               for(int e=first[i];~e;e=nex[e])
               if(g[v[e]][j])r++;
               ans+=r*(r-1)/2;
           }
        }
        printf("%d\n",ans);
    }
    return 0;
}

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 abc 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:


Codeforces Round #277.5 (Div. 2)-D「建议收藏」

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 nm (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 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115961.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月28日 上午7:00
下一篇 2022年1月28日 上午7:00


相关推荐

  • 云大使推广中的常见热门问题「建议收藏」

    云大使推广中的常见热门问题「建议收藏」云大使推广中的常见热门问题

    2022年4月21日
    70
  • js对象与json字符串的互转

    js对象与json字符串的互转今天在面试的时候 面试官问到 js 对象与 json 字符串的互转问题 由于自己平时在用的时候没有在意 而且在工作中由于用的较少 没有答上来 所以今天特地查查资料复习一下 巩固一下这方面的知识 1 js 对象转换成 json 字符串在与后端进行交互时 有时需要把 js 对象转换成 json 字符串格式 这时我们需要去引用一下 json2 js 这个文件 然后调用 JSON stringify 方法

    2026年3月17日
    2
  • Apache POI表格无法使用pandas打开Excel报错Workbook contains no default style, apply openpyxl‘s default

    Apache POI表格无法使用pandas打开Excel报错Workbook contains no default style, apply openpyxl‘s default打不开产生原因是因为 xlsx 文件是由 ApachePOI 创建 并不是 MicrosoftExc 创建 从文件属性里面程序名称是 ApachePOI 可以看到 解决办法一手动打开 excel 并重新保存或者另存为即可 然后就可以使用 pandas 打开了 解决办法二使用 win32com 加载 Excel 应用 然后重新保存即可 使用前先安装 pipinstallpy coding utf 8fromwin32co client

    2026年3月18日
    1
  • Idea 全局搜索(idea如何全局搜索关键字)

    在使用Eclipse的时候用到了全局查找功能Ctrl+H,还是非常好用的, 在IDEA中同样有全局搜索功能,我用的是Eclipse版本的快捷键,是Ctrl+H。    特此查找记录,分享。…

    2022年4月18日
    528
  • oracle 分页查询 优化_oracle分页查询封装

    oracle 分页查询 优化_oracle分页查询封装对于数据库中表的数据的 Web 显示,如果没有展示顺序的需要,而且因为满足条件的记录如 此之多,就不得不对数据进行分页处理。常常用户并不是对所有数据都感兴趣的,或者大部分情 况下,他们只看前几页。 通常有以下两种分页技术可供选择。 1234567Select * from (Select rownumrn,t.* from table t)Where rn>&minnum and rn或者Sel

    2025年7月18日
    9
  • OpenSSL密码库算法笔记——第5.4.13章 椭圆曲线点的压缩

    OpenSSL密码库算法笔记——第5.4.13章 椭圆曲线点的压缩首先来看看什么是点的压缩。椭圆曲线上的任一仿射点(x,y)(非无穷远点)都可以压缩成利用其y坐标的最后一比特(记为y*)和x坐标来表示,即(x,y*),这就是点的压缩。反过来,利用(x,y*)恢复y坐标,还原仿射点(x,y)的过程就称为点的解压缩。利用点的压缩可以减少存储和传输时的数据量,但增加了数据处理时间。代码中用参数point_conver…

    2022年7月20日
    14

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号