HDU-6184 Counting Stars(暴力找三元环)

Counting Stars

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1812    Accepted Submission(s): 504

Problem Description

Little A is an astronomy lover, and he has found that the sky was so beautiful!

So he is counting stars now!

There are n stars in the sky, and little A has connected them by m non-directional edges.

It is guranteed that no edges connect one star with itself, and every two edges connect different pairs of stars.

Now little A wants to know that how many different "A-Structure"s are there in the sky, can you help him?

An "A-structure" can be seen as a non-directional subgraph G, with a set of four nodes V and a set of five edges E.

If V=(A,B,C,D) and E=(AB,BC,CD,DA,AC), we call G as an "A-structure".

It is defined that "A-structure" G1=V1+E1 and G2=V2+E2 are same only in the condition that V1=V2 and E1=E2.

Input

There are no more than 300 test cases.

For each test case, there are 2 positive integers n and m in the first line.

2≤n≤105, 1≤m≤min(2×105,n(n−1)2)

And then m lines follow, in each line there are two positive integers u and v, describing that this edge connects node u and node v.

1≤u,v≤n

∑n≤3×105,∑m≤6×105

Output

For each test case, just output one integer--the number of different "A-structure"s in one line.

Sample Input

4 5 1 2 2 3 3 4 4 1 1 3 4 6 1 2 2 3 3 4 4 1 1 3 2 4

Sample Output

1 6

Source

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

题意:问有多少个子图,包含4个顶点5条边

题解:问题可以转换为:问有多少个子图,包含2个相交的三元环(共用一条边)

可以直接用O(msqrt(m))的方法找三元环,假设第i条边经过了cnt[i]个三元环,那么贡献就是cnt[i]*(cnt[i]-1)/2

不过这是个可耻的卡常题,我用unordered_map结果MLE了。改成unordered_set才过了。

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'<<endl
#define clr(x,y) memset(x,y,sizeof(x))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MX = 1e5 + 5;
unordered_set<ll>s;
int cnt[MX << 1];
PII edge[MX << 1];
VI E[MX];
inline ll code(int u, int v) {
    return u < v ? (ll)u * MX + v : (ll)v * MX + u;
}
int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    int n, m;
    while(~scanf("%d%d", &n, &m)) {
        rep(i, 1, n + 1) E[i].clear();
        s.clear();
        rep(i, 1, m + 1) {
            int u, v;
            scanf("%d%d", &u, &v);
            s.insert(code(u, v));
            edge[i] = PII(u, v);
            E[u].pb(v);
            E[v].pb(u);
            cnt[i] = 0;
        }
        ll ans = 0;
        rep(i, 1, m + 1) {
            int u = edge[i].x, v = edge[i].y;
            if(E[u].size() > E[v].size()) swap(u, v);
            for(auto w : E[u]) {
                if(s.count(code(v, w))) cnt[i]++;
            }
            ans += (ll) cnt[i] * (cnt[i] - 1) / 2;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值