题干:

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)V=(A,B,C,D) and E=(AB,BC,CD,DA,AC)E=(AB,BC,CD,DA,AC), we call G as an "A-structure".

It is defined that "A-structure" G1=V1+E1G1=V1+E1 and G2=V2+E2G2=V2+E2 are same only in the condition that V1=V2V1=V2 and E1=E2E1=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≤1052≤n≤105, 1≤m≤min(2×105,n(n−1)2)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≤n1≤u,v≤n

∑n≤3×105∑n≤3×105,∑m≤6×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

题目大意:

    

解题报告:

暴力。这题主要是卡空间了啊不然随便搞的。。然后卡空间了变成怎么交都MLE、、、

对于题干,不难想到就是问有多少个双三元环,所有我们枚举每一条边,然后能构成的所有三元环个数假设x,那就是C(2,x)就是这条边的贡献。

暴力每个点i,然后枚举他的所有边获得点j,然后对于第三个点k分两种情况讨论:

①如果j的临边数目少于,那么直接暴力就好,复杂度

②如果临边数目大于,(这样的点一定不会多于的复杂度  个)那么暴力i的邻接点,二分判断有没有就可以了。

这样保证总复杂度

好像还有更优秀的做法:参考博客

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX];
int e[MAX];
int n,m;
int main()
{while(~scanf("%d%d",&n,&m)) {for(int i = 1; i<=n; i++) e[i] = -1,vv[i].clear();for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);if(u>v) swap(u,v);vv[u].pb(v);vv[v].pb(u);}for(int i = 1; i<=n; i++) sort(vv[i].begin(),vv[i].end());int SQRT = sqrt(m);ll ans = 0;for(int i = 1; i<=n; i++) {for(auto j : vv[i]) e[j] = i;for(auto j : vv[i]) {if(j > i) {int sz = vv[j].size(),cnt = 0;if(sz <= SQRT) {for(auto k : vv[j]) cnt += e[k] == i;}else {                        for(auto k : vv[i]) cnt += binary_search(vv[k].begin(),vv[k].end(),j); }ans += 1LL*cnt*(cnt-1)/2;}}}printf("%lld\n",ans);}return 0 ;
}

还有一个问题啊、、为什么sz<SQRT的话,就一直报TLE啊????欢迎来讨论、、、

  • 焦作 L
  • Commet oj的一道题
  • 牛客多校第三场A

改成严格的msqrtm的算法:(建有向边)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int cnt[MAX];
int U[MAX],V[MAX],du[MAX];
vector<PII> vv[MAX];
PII e[MAX];
int n,m;
int main()
{while(~scanf("%d%d",&n,&m)) {for(int i = 1; i<=n; i++) du[i] = 0,e[i].FF = 0,vv[i].clear();for(int i = 1; i<=m; i++) cnt[i] = 0;for(int i = 1; i<=m; i++) scanf("%d%d",U+i,V+i),du[U[i]]++,du[V[i]]++;for(int u,v,i = 1; i<=m; i++) {u = U[i],v = V[i];if(du[u] > du[v]) vv[v].pb(pm(u,i));else if(du[u] < du[v]) vv[u].pb(pm(v,i));else {if(u<v) vv[u].pb(pm(v,i));else vv[v].pb(pm(u,i)); }}for(int u,v,i = 1; i<=m; i++) {u = U[i],v = V[i];for(auto x : vv[u]) e[x.FF] = pm(i,x.SS);for(auto x : vv[v]) {if(e[x.FF].FF == i) cnt[i]++,cnt[x.SS]++,cnt[e[x.FF].SS]++;} }ll ans = 0;for(int i = 1; i<=m; i++) ans += 1LL*cnt[i]*(cnt[i]-1)/2;printf("%lld\n",ans);}return 0 ;
}

【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论)相关推荐

  1. HDU - 6184 Counting Stars(思维+三元环)

    题目链接:点击查看 题目大意:给出一个 nnn 个点 mmm 条边组成的无向图,问图中有多少个"三元环对","三元环对"指的是两个三元环共用了一条边 题目分析: ...

  2. 牛客挑战赛51 E NIT的gcd(欧拉反演,建图优化,三元环计数)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Problem 给你一个正整数 nnn. 请你输出 ∑i=1n∑j=1n∑k=1ngcd⁡(i,j)g ...

  3. P4619 [SDOI2018]旧试题(莫比乌斯反演,建图优化三重枚举,三元环计数,神仙好题,超级清晰易懂)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 P4619 [SDOI2018]旧试题(莫比乌斯反演,三元环计数) Problem 计算: ∑i=1A ...

  4. 三元环计数四元环计数

    三元环计数 问题 给出一张n个点m条边的无向图,问图中有多少个三元组{ u , v , w } ,满足图中存在 { (u,v) , (v,w) , (w,u) } 三条边. 求解 Step1 定向 将 ...

  5. HDU6184【Counting Stars】(三元环计数)

    题面 传送门 给出一张无向图,求 \(4\) 个点构成两个有公共边的三元环的方案数. 题解 orz余奶奶,orz zzk 首先,如果我们知道经过每条边的三元环个数\(cnt_i\),那么答案就是\(\ ...

  6. 洛谷 - P1989 无向图三元环计数(思维建图)

    题目链接:点击查看 题目大意:给出一个 nnn 个点 mmm 条边组成的无向图,求三元环的个数 题目分析:对于原图建新图,对于原来的每条边来说 如果度数不同,度数小的点指向度数大的点 如果度数相同,编 ...

  7. P1989 无向图三元环计数 思维 + 建图

    传送门 文章目录 题意: 思路: 题意: 统计无向图中三元环的个数. 思路: 很明显有一种暴力的方法,就是枚举每条边,让后再跑两个点的所有边,可以卡到复杂度O(m2)O(m^2)O(m2). 我们可以 ...

  8. 基站建设(三元环计数+根号分治 / bitset)

    基站建设 problem solution code problem 给定 nnn 个地点,以及每个地点的可靠度 RiR_iRi​. 有 mmm 条光纤架,每一条连接两个不同的地点,且是双向的. 测试 ...

  9. 【Codeforces - 找不到题号】三元环计数(bitset优化,压位)

    题干: 给你一个二维字符矩阵,如果 ( i , j ) 为+ 表明 两点之间有一条有向边,为-表示没有边,那么你要找出所有的三元环的个数.顶点数N<=1500. 解题报告: 考虑最暴力的方法,开 ...

最新文章

  1. dotNet core Windows上 部署
  2. 震惊!原来这才是Kafka的“真面目”!
  3. CAN'T TAKE MY EYES OF YOU
  4. 面向对象程序设计课程进度条
  5. mysql 的 sql_mode.only_full_group_by属性解析
  6. aqs clh java_Java并发包源码学习之AQS框架(二)CLH lock queue和自旋锁
  7. 【英语学习】【WOTD】impetus 释义/词源/示例
  8. python个人所得税怎么写分录_个人所得税的会计分录!
  9. VirtualBox虚拟机如何扩容
  10. 损失函数、tensorflow2实现——Python实战
  11. 10月11 小结: 你又贪玩了不是?
  12. 凸优化第五章对偶 5.2 Lagrange对偶问题
  13. 计算机锁定无法安装软件,无法安装软件是什么原因,Win10无法安装应用软件的处理方法...
  14. 尼尔机械纪元免安装中文 2B的姐姐单机游戏 NieR Automata +修改器解锁存档
  15. Android Studio入门到精通
  16. 报错undefined symbol: _ZN3c104impl23ExcludeDispatchKeyGuardC1ENS_11DispatchKeyE
  17. 【C语言程序】求直角三角形边长
  18. 什么情况下选用mysql_在MySQL中,‘%’可以用在什么情况下?
  19. 安装Python包,网络不可达解决方法
  20. 【案例】 压缩算法 —— LZ 算法

热门文章

  1. ASP.NET使用Memcached高缓存实例(初级教程)
  2. [剑指offer][JAVA]面试题第[32-3]题[从上到下打印二叉树 ][BFS]
  3. 1115. Counting Nodes in a BST (30) 数据结构
  4. 平面设计中的网格系统pdf_深入浅出,带你认识网格系统与版式设计
  5. jedispool redis哨兵_Redis详解(九)------ 哨兵(Sentinel)模式详解
  6. 检查mysql的replication_MySQL Replication需要注意的问题
  7. tidb vs mysql_一个长耗时SQL在TiDB和Mysql上的耗时测试
  8. UE4 查看打包文件内容
  9. UE4 HTC VIVE - 番外篇 - 局域网联机三
  10. A5D2 GPIO测试