当前位置 : 主页 > 编程语言 > c语言 >

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)C - Counting Stars 暴力三元环

来源:互联网 收集:自由互联 发布时间:2023-09-06
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

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

暴力找即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 100005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
#define lson 2*x
#define rson 2*x+1

long long  qupower(int a, int b) {
    long long  ans = 1;
    while (b) {
        if (b & 1)ans = ans * a;
        b >>= 1;
        a = a * a;
    }
    return ans;
}

inline int read() {
    int an = 0, x = 1; char c = getchar();
    while (c > '9' || c < '0') {
        if (c == '-') {
            x = -1; 
        }
        c = getchar();
    }
    while (c >= '0'&&c <= '9') {
        an = an * 10 + c - '0'; c = getchar();
    }
    return an * x;
}

vector<int>G[maxn];
set<ll>st;
int vis[maxn], lnk[maxn], ot[maxn];


int main() {
    ios::sync_with_stdio(false);
    int n, m;
    ll ans, sum;
    while (cin >> n >> m) {
        int i, j;
        ans = 0; sum = 0;
        int part = sqrt(m);

        for (i = 1; i <= n; i++)G[i].clear();
        ms(vis); st.clear(); ms(lnk); ms(ot);
        for (i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            G[u].push_back(v); ot[u]++;
            G[v].push_back(u); ot[v]++;
            st.insert((ll)u*n + v);
            st.insert((ll)v*n + u);
        }
        for (i = 1; i <= n; i++) {
            int x = i;
            vis[x] = 1;
            for (j = 0; j < G[x].size(); j++) {
                lnk[G[x][j]] = x;
            }

            for (j = 0; j < G[x].size(); j++) {
                sum = 0;
                int y = G[x][j];
                if (vis[y])continue;
                if (ot[y] <= part) {
                    for (int k = 0; k < G[y].size(); k++) {
                        if (lnk[G[y][k]] == x)sum++;
                    }
                }
                else {
                    for (int k = 0; k < G[x].size(); k++) {
                        int z = G[x][k];
                        if (st.find((ll)z*n + y) != st.end())sum++;
                    }
                }
                ans += 1ll*(sum - 1)*sum / 2;
            }
        }
        cout << ans << endl;
    }
}
上一篇:ECNA 2017 Problem G: A Question of Ingestion DP
下一篇:没有了
网友评论