2017ACMICPC广西赛C题,如何暴力统计三元环?

2026-04-11 22:042阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计885个文字,预计阅读时间需要4分钟。

2017ACM/ICPC广西赛C题,如何暴力统计三元环?

小A是一位天文爱好者,他发现天空如此美丽!于是他开始数星星。天空中共有n颗星星,小A用m条无向边将它们连接起来。保证没有任何边连接同一颗星星。

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

2017ACM/ICPC广西赛C题,如何暴力统计三元环?

∑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; } }

本文共计885个文字,预计阅读时间需要4分钟。

2017ACM/ICPC广西赛C题,如何暴力统计三元环?

小A是一位天文爱好者,他发现天空如此美丽!于是他开始数星星。天空中共有n颗星星,小A用m条无向边将它们连接起来。保证没有任何边连接同一颗星星。

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

2017ACM/ICPC广西赛C题,如何暴力统计三元环?

∑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; } }