Counting Stars HDU - 6184
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
无向图三元环详解
这里需要一些小小的改动
题目中要求的A结构其实是两个具有一条公共边的三元环
求这样的个数
根据算法我们一开始就是先枚举一条边,这样我们就以第一条边为公共边,找出三元环的个数,然后 C2sum C s u m 2 从这n个中选2个即可,也就是 sum⋅(sum−1)2 s u m ⋅ ( s u m − 1 ) 2
code:
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
vector<int>G[maxn];
set st;
int vis[maxn],link[maxn],out[maxn];
int main(){
ll ans,sum;
int n,m,i,j,k,x,y,z,B;
while(scanf("%d%d",&n,&m) != EOF){
//初始化
B = sqrt(m);
st.clear();
for(i = 1; i <= n; i++){
G[i].clear();
vis[i] = out[i] = link[i] = 0;
}
for(i = 1; i <= m; i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
out[x]++;
G[y].push_back(x);
out[y]++;
st.insert((ll)x*n+y);
st.insert((ll)y*n+x);
}
ans = 0;
for(i = 1; i <= n; i++){
x = i;
vis[x] = 1;
for(j = 0; j < G[x].size(); j++)
link[G[x][j]] = x;
for(j = 0; j < G[x].size(); j++){
sum = 0;
y = G[x][j];
if(vis[y])
continue;
if(out[y] <= B){
for(k = 0; k < G[y].size(); k++){
z = G[y][k];
if(link[z] == x)
sum++;
}
}
else{
for(k = 0; k < G[x].size(); k++){
z = G[x][k];
if(st.find((ll)z*n+y) != st.end())
sum++;
}
}
ans += sum * (sum - 1) / 2;
}
}
printf("%lld\n",ans);
}
return 0;
}