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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cmos出现问题_sensor和cmos

    cmos出现问题_sensor和cmos在某些场景下,使用者可以看到相机画面出现一条一条的滚动暗条纹,如下图片所示,这样的现象,通常是CMOSsensor曝光时间方面的因素引起的。

    2022年10月13日
    2
  • CSS实现实心三角形和空心三角形[通俗易懂]

    CSS实现实心三角形和空心三角形[通俗易懂]一次开发中遇到,记录代码原理:1.给一个div,宽和高都为0的时候,盒子什么都没有看起来。为空白2.给一个宽高为0的盒子给一遍像素给100px的上边,下边和右边,border-top:90pxsolidred;border-right:100pxsolidblack;border-bottom:100pxsolidblue;这样左边没有,就会缩成一

    2022年6月29日
    29
  • hostapd.conf详细

    hostapd.conf详细#####hostapdconfigurationfile###############################################Emptylinesandlinesstartingwith#areignored#APnetdevicename(without’ap’postfix,i.e.,wlan0useswlan0a…

    2022年5月22日
    186
  • 爬虫工具_应用程序market

    爬虫工具_应用程序market一个简单的异步爬虫.私信太多,统一回答一下:关于异步函数的:1.真正派发任务的是consumer这个coroutine,所以也在内部做了并发控制.2.process_content用于获取html及保存到mysql.关于异步相关(asyncio)的:1.await相当于yieldfrom.2.await后面是一个coroutine,…

    2025年7月26日
    5
  • idea2019激活教程,永久激活,一次性搞定!(必看)

    idea2019激活教程,永久激活,一次性搞定!(必看) 此教程仅用作个人学习,请勿用于商业获利,造成后果自负!!! 此教程已支持最新2019.2版本 永久激活方法 1.下载jar包 点击链接 网盘链接:pan.baidu.com/……

    2022年3月13日
    82
  • windows7系统 您的账户已被停用。请向系统管理员咨询

    windows7系统 您的账户已被停用。请向系统管理员咨询问题细节描述:前几天好像是想换个用户桌面,换个用户桌面,首先把Administrator用户给禁用,然后把现在使用的用户名给删除。重启电脑,结果进不去了,显示这个错误提示:您的账户已被停用。请向系统管理员咨询解决办法:1.首先重启–(正常启动)2.按F8–(这个大家都知道-开机选项)3.选择安全模式–(注意:不是带命令的安全模式,是安全模式。F8第

    2025年6月23日
    3

发表回复

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

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