$n$ 点 $m$ 边的图求多少对三元环公用一条边
变无向图为有向图

建图方法:
对于每条无向边 度数小的端点向度数大的端点连边
度数相同则编号小的点向编号大的点连边
这样就构成 $DAG$
遍历:
遍历每条边的 $u$
标记另一端点 $v$
遍历该边的 $v$
如果 $v$ 的 $v_2$ 的标记与 $v$ 相同
则说明构成了三元环 $(u, v), (v, v_2), (u, v_2)$

记录每条边构成的三元环的个数 $x$
组合数 $x \choose 2$ 加入答案

由于连边时每个点的出边都要 $<= \sqrt(m)$

所以时间复杂度 $O(m \sqrt(m))$

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <cstdlib> using namespace std; #define gc getchar()
inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
}
#undef gc const int N = 1e6 + , M = 2e5 + ; int du[N], a[M], b[M];
int n, m, head[N], cnt, Ans[N];
struct Node {int v, nxt, id;} G[M];
struct Node2 {int first, second;} vis[N]; inline void Add(int u, int v, int id) {
G[++ cnt].v = v; G[cnt].nxt = head[u]; G[cnt].id = id; head[u] = cnt;
} void Clear() {
cnt = ;
memset(vis, , sizeof vis);
memset(du, , sizeof du);
memset(Ans, , sizeof Ans);
for(int i = ; i <= n; i ++) head[i] = -;
} int main() {
while(scanf("%d %d", &n, &m) == ) {
Clear();
for(int i = ; i <= m; i ++) {
a[i] = read(), b[i] = read();
du[a[i]] ++, du[b[i]] ++;
}
for(int i = ; i <= m; i ++) {
if(du[a[i]] > du[b[i]] || (du[a[i]] == du[b[i]] && a[i] > b[i])) swap(a[i], b[i]);
Add(a[i], b[i], i);
}
int use_num = ;
for(int i = ; i <= m; i ++) {
use_num ++;
for(int j = head[a[i]]; ~ j; j = G[j].nxt) {
vis[G[j].v] = (Node2) {use_num, G[j].id};
}
for(int j = head[b[i]]; ~ j; j = G[j].nxt) {
if(vis[G[j].v].first == use_num) {
Ans[i] ++, Ans[G[j].id] ++, Ans[vis[G[j].v].second] ++;
}
}
}
long long answer = ;
for(int i = ; i <= m; i ++) if(Ans[i] > ) answer += (Ans[i] * (Ans[i] - ) / );
printf("%lld\n", answer);
}
return ;
}

最新文章

  1. Eclipse 各版本版本号代号对应一览表
  2. 利用ShareSDK进行第三方登录和分享
  3. Unity3D入门之Unity3D介绍以及编辑器的使用(1)
  4. 【1】第一次电话面试---上海EMC
  5. Python类库下载
  6. Ext.Loader
  7. Unity3d之UGUI- Image拦截Button响应事件
  8. 50道经典的JAVA编程题 (16-20)
  9. new static() 和 new self() 的区别异同
  10. java 内存 垃圾回收调优
  11. php 利用第三方软件进行网页快照
  12. .net 网站发布 Web.Config中的&lt;compilation debug=&quot;true&quot;/&gt;
  13. Let&#39;s Format Css Documents
  14. iOS 基于Socket 的 C/S 网络通信结构(下一个)
  15. 一款代码扫描工具 火线!!!! fireline
  16. C#设计模式之六原型模式(Prototype)【创建型】
  17. auto-encoder小记
  18. scala程序开发入门
  19. MOOC Linux内核之旅小结【转】
  20. centos 7安装tomcat

热门文章

  1. Python+VSCode+Git【转】
  2. java之aop使用及自定义注解
  3. MySQL 索引机制
  4. hdu 2822 ~!!!!!!坑死我
  5. VS.NET(C#)--2.9_HTML服务器控件案例
  6. NOPI 读与写
  7. physdiskwrite 的简单使用
  8. Go net/http,web server
  9. iOS - 总结适配IOS10需要注意的问题
  10. 开始Swift学习之路