hdu 6184 Counting Stars(三元环计数)

题意:

给一张n个点m条边的无向图,问有多少个\(A-structure\)

其中\(A-structure\)满足\(V=(A,B,C,D)\) && \(E=(AB,BC,CD,DA,AC)\)

显然\(A-structure\)是由两个有公共边的三元环构成的

\(1 <=n <= 1e5\)

\(1 <= m <= min(2e5,n*(n-1)/2)\)

思路:

三元环计数

做法1、

①统计每个点的度数

②入度\(<=sqrt(m)\)的分为第一类,入度\(>sqrt(m)\)的分为第二类

③对于第一类,暴力每个点,然后暴力这个点的任意两条边,再判断这两条边的另一个端点是否连接

因为\(m\)条边最多每条边遍历一次,然后暴力的点的入度\(<=sqrt(m)\),所以复杂度约为\(O(msqrt(m))\)

④对于第二类,直接暴力任意三个点,判断这三个点是否构成环,因为这一类点的个数不会超过\(sqrt(m)\)个,所以复杂度约为\(O(sqrt(m)^3)=O(msqrt(m))\)

⑤判断两个点是否连接可以用set,map和Hash都行,根据具体情况而行

这种做法建的是双向边,常数很大

更优的做法2、建有向边 复杂度为\(O(msqrt(m))\)

对所有边按照两端点的度数定向,度数小的往度数大的走,度数相同则按编号小到大走,这样定向后

可以保证是个有向无环图。

为什么呢,要想定向存在环,则这个环上的点度数必须相同,由于保证了编号从小到大走

所以是不可能存在环的。

这样定向同时还保证了每个点的出度不超过\(sqrt(m)\),很容易证明,如果存在一个点出度超过了\(sqrt(m)\),则说明存在其他\(sqrt(m)\)个点的度数\(>sqrt(m)\),算起来超过边数\(m\)了。

对于这道题,我们在求三元环的时候,统计一下每条边有多少对点能构成三元环,\(C(cnt,2)\)累计一下即可

做法1、

#include<bits/stdc++.h>
#define LL long long using namespace std;
void read(int &x){
x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
}
const int N = 1e5 + 10;
set<LL> g;
int deg[N];
vector<int> G[N];
int vis[N],vi[N];
int main(){ int n, m, u, v, Sz;
while(scanf("%d%d",&n,&m) != EOF){
Sz = sqrt(m + 0.5);
g.clear();
for(int i = 1;i <= n;i++){
vis[i] = vi[i] = deg[i] = 0;
G[i].clear();
}
for(int i = 0;i < m;i++){
scanf("%d%d",&u,&v);
g.insert(u + 1LL * v * n);
g.insert(v + 1LL * u * n);
deg[u]++,deg[v]++;
G[u].push_back(v);
G[v].push_back(u);
}
LL ans = 0;
for(int u = 1;u <= n;u++){
vis[u] = 1;
for(auto v:G[u]) vi[v] = u;
for(auto v:G[u]){
int cnt = 0;
if(vis[v]) continue;
if(deg[v] <= Sz){
for(auto vv:G[v]){
if(vi[vv] == u) cnt++;
}
}else{
for(auto vv:G[u]){
if(g.find(1LL * v * n + vv) != g.end()) cnt++;
}
}
ans += 1LL * cnt * (cnt - 1) / 2;
}
}
printf("%lld\n",ans);
}
return 0;
}

做法二、

#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
using namespace std;
void read(int &x){
x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
}
const int N = 1e5 + 10;
set<LL> g;
int deg[N];
vector<pair<int,int> > G[N];
int vi[N];
int X[N * 2],Y[N * 2],cnt[N],pos[N];
int main(){
int n, m, u, v, Sz;
while(scanf("%d%d",&n,&m) != EOF){
Sz = sqrt(m);
for(int i = 1;i <= n;i++){
vi[i] = deg[i] = pos[i] = 0;
G[i].clear();
}
g.clear();
int tot = 0;
for(int i = 0;i < m;i++){
scanf("%d%d",&X[i],&Y[i]);
u = X[i],v = Y[i];
deg[u]++,deg[v]++;
}
for(int i = 0;i < m;i++){
cnt[i] = 0;
if(deg[X[i]] < deg[Y[i]]) G[X[i]].push_back(make_pair(Y[i],i));
else if(deg[Y[i]] < deg[X[i]]) G[Y[i]].push_back(P(X[i],i));
else{
if(X[i] < Y[i]) G[X[i]].push_back(P(Y[i],i));
else G[Y[i]].push_back(P(X[i],i));
}
}
LL ans = 0;
for(int i = 0;i < m;i++){
u = X[i],v = Y[i];
for(auto vp:G[u]) pos[vp.first] = vp.second,vi[vp.first] = i + 1;
for(auto vp:G[v]){
int vv = vp.first;
if(vi[vv] == i + 1){
cnt[i]++;
cnt[pos[vv]]++;
cnt[vp.second]++;
}
}
}
for(int i = 0;i < m;i++) ans += 1LL * cnt[i] * (cnt[i] - 1) / 2;
printf("%lld\n",ans);
}
return 0;
}

[hdu 6184 Counting Stars(三元环计数)的更多相关文章

  1. HDU 6184 Counting Stars

    Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful!S ...

  2. HDU 6184 Counting Stars 经典三元环计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6184 题意: n个点m条边的无向图,问有多少个A-structure 其中A-structure满足V ...

  3. 【刷题】HDU 6184 Counting Stars

    Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful! ...

  4. Codechef SUMCUBE Sum of Cubes 组合、三元环计数

    传送门 好久没有做过图论题了-- 考虑\(k\)次方的组合意义,实际上,要求的所有方案中导出子图边数的\(k\)次方,等价于有顺序地选出其中\(k\)条边,计算它们在哪一些图中出现过,将所有方案计算出 ...

  5. 【BZOJ5332】[SDOI2018]旧试题(数论,三元环计数)

    [BZOJ5332][SDOI2018]旧试题(数论,三元环计数) 题面 BZOJ 洛谷 题解 如果只有一个\(\sum\),那么我们可以枚举每个答案的出现次数. 首先约数个数这个东西很不爽,就搞一搞 ...

  6. loj#6076「2017 山东一轮集训 Day6」三元组 莫比乌斯反演 + 三元环计数

    题目大意: 给定\(a, b, c\),求\(\sum \limits_{i = 1}^a \sum \limits_{j = 1}^b \sum \limits_{k = 1}^c [(i, j) ...

  7. BZOJ.5407.girls/CF985G. Team Players(三元环计数+容斥)

    题面 传送门(bzoj) 传送门(CF) \(llx\)身边妹子成群,这天他需要从\(n\)个妹子中挑出\(3\)个出去浪,但是妹子之间会有冲突,表现为\(i,j\)之间连有一条边\((i,j)\), ...

  8. LOJ2565 SDOI2018 旧试题 莫比乌斯反演、三元环计数

    传送门 这道题的思路似乎可以给很多同时枚举三个量的反演题目提供一个很好的启发-- 首先有结论:\(d(ijk) = \sum\limits_{x|i}\sum\limits_{y|j}\sum\lim ...

  9. hdu6184 Counting Stars 【三元环计数】

    题目链接 hdu6184 题解 题意是让我们找出所有的这样的图形: 我们只需要求出每条边分别在多少个三元环中,记为\(x\),再然后以该点为中心的图形数就是\({x \choose 2}\) 所以我们 ...

随机推荐

  1. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  2. 系统架构的定义(与系统)-architecture

    architecture⟨system⟩ fundamental concepts or properties of a system in its environment embodied in i ...

  3. SpringMVC(1)

    1.简要说明: Spring为展现层提供的基于MVC设计理念的优秀web框架,目前主流的框架 Spring3.0以后全面超越Struts2,成为最优秀的MVC框架 Spring MVC通过一套MVC注 ...

  4. 【luogu P1666 前缀单词】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1666 10.13考试题 当时没想出来,觉得是要用trie做,在trie上跑一个树形dp 结果是写了个子集枚举 ...

  5. HTML中IMG标签总结

    一.Img标签有两个重要的属性: 1.src  属性:图片的地址 2.alt  属性:图片不显示是现实的文字 二.Img标签是行级元素: img.input属于行内替换元素.height/width/ ...

  6. HDU 5536--Chip Factory(暴力)

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  7. hdu_5187_zhx's contest

    Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...

  8. Mybartis逆向工程

    Mybartis逆向工程 0.创建工程项目,切记莫用中文,亲测在运行时报错 1.Pom文件,使用mybatis-generator插件 <?xml version="1.0" ...

  9. 复习宝典之Git分布式版本控制

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第三章:Git分布式版本控制 1)git文件状态 git中的文件有以下几种状态: 未跟踪(untrack):表示文件为新增加的. 已修改 ...

  10. Windows 安装 MongoDB 并开启认证

    下载 可以自行上官网找需要的版本,Windows系统各个64位版本下载地址: http://dl.mongodb.org/dl/win32/x86_64 安装 正常的软件安装流程,这里就不细讲了. 配 ...