1. 程式人生 > >HDU-6184 Counting Stars(暴力找三元環)

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

題意:問有多少個子圖,包含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;
}