首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Codeforces Round #277.5 (Div. 2)-D「建议收藏」

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

作者头像
全栈程序员站长
发布于 2022-07-08 11:30:42
发布于 2022-07-08 11:30:42
3310
举报

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

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

代码:

代码语言:javascript
AI代码解释
复制
#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 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 test(s)

input

代码语言:javascript
AI代码解释
复制
5 4
1 2
2 3
1 4
4 3

output

代码语言:javascript
AI代码解释
复制
1

input

代码语言:javascript
AI代码解释
复制
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

代码语言:javascript
AI代码解释
复制
12

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档