HDU 6184 Counting Stars

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

题意:给定一张无向图,求有公共边的三元环对数。

Solution:

  三元环裸题。

  直接三元环计数,然后开一个桶记录一下每条边在多少个三元环中出现,最后的答案就是$\sum_\limits{i=1}^{i\leq m}{\frac{tot[i]*(tot[i]-1)}{2}}$。

代码:

/*Code by 520 -- 9.10*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=100005,M=200005;
int n,m,to[M],net[M],h[N],cnt,tot[M],pre[N],vis[N],deg[N];
struct node{
    int u,v;
}e[M];
ll ans;

il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;}

int main(){
    while(scanf("%d%d",&n,&m)==2){
        For(i,1,m) scanf("%d%d",&e[i].u,&e[i].v),deg[e[i].u]++,deg[e[i].v]++;
        For(i,1,m) {
            RE int u=e[i].u,v=e[i].v;
            if(deg[u]<deg[v]||deg[u]==deg[v]&&u>v) swap(u,v);
            add(u,v);
        }
        For(u,1,n){
            for(RE int i=h[u];i;i=net[i]) vis[to[i]]=u,pre[to[i]]=i;
            for(RE int i=h[u];i;i=net[i]){
                RE int v=to[i];
                for(RE int j=h[v];j;j=net[j]){
                    RE int w=to[j];
                    if(vis[w]==u) ++tot[i],++tot[j],++tot[pre[w]];
                }
            }
        }
        For(i,1,cnt) ans+=1ll*tot[i]*(tot[i]-1)/2;
        printf("%lld\n",ans);
        memset(h,0,sizeof(h)),memset(deg,0,sizeof(deg)),
        memset(tot,0,sizeof(tot)),memset(pre,0,sizeof(pre)),
        memset(vis,0,sizeof(vis)),cnt=0,ans=0;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/five20/p/9623592.html

时间: 2024-10-10 05:26:38

HDU 6184 Counting Stars的相关文章

HDU 6184 Counting Stars 经典三元环计数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6184 题意: n个点m条边的无向图,问有多少个A-structure 其中A-structure满足V=(A,B,C,D) && E=(AB,BC,CD,DA,AC) 解法: 可以看出A-structure是由两个有公共边的三元环构成的,然后就变成了这道题. http://www.cnblogs.com/spfa/p/7495438.html #include <stdio.h>

三元环HDU 6184

HDU - 6184 C - Counting Stars 题目大意:有n个点,m条边,问有一共有多少个‘structure’也就是满足V=(A,B,C,D) and E=(AB,BC,CD,DA,AC)这样一个图形,类似于四边形中间连接了一条对角线. 如果我们把这个四边形拆分的话,其实就是两个共用一条边的三角形,而在图中就是三元环.求三元环有两种求法,个人感觉这个三元环的时间复杂度很玄学. 第一种一种就是枚举点x,然后枚举和点x相连的y,这时根据y的度,如果y的度小于等于sqrt(m),那么我

hdu 1396 Counting Triangles (递推)

Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 966 Problem Description Given an equilateral triangle with n the length of its side, program to

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in t

@hdu - [email&#160;protected] Counting Stars

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 给定一个 n 点 m 边的无向图(无重边自环),求有多少子图形如,包含 4 个点 {A, B, C, D} 与 6 条边 {AB, BC, CD, DA, AC}. 原题链接. @[email protected] 一个并不常用的黑科技:三元环计数. mark一下博客地址. 注意到题目

hdu 2952 Counting Sheep

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2952 Counting Sheep Description A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'

HDU 1264 Counting Squares(线段树求面积的并)

Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1885    Accepted Submission(s): 946 Problem Description Your input is a series of rectangles, one per line. Each rectangle is sp

HDU 5862 Counting Intersections

题目:Counting Intersections 链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 题意:给出n 条平行于坐标轴的线段,问这些线段有多少交点(题目保证没有两条线段共享一个端点.保证没有重叠.保证线段长度大于0)n范围10万 思路: 将n 条线段2n 个点按x 排序,然后当遇到一个横向的左端点时,对应的y++,遇到右端点,y--,遇到竖线,交点数目加上 下端点到上端点 之间的y 的和.离散化加树状数组可做.要注意细节,

HDU 5533 Dancing Stars on Me 计算几何瞎暴力

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1184    Accepted Submission(s): 651 Problem Description The sky was brushed clean by the wind and the stars were cold in a b