【图论】无向图数圈圈
本篇主要讨论圈数较小(
1.k 4
这部分可以做到
容易发现每个三元环都是
判断很简单,每次枚举
看起来很暴力对不对?其实这样是
我们考虑每次是枚举了一条边,和这条边出点的出度,尝试证明每个点的出度都是
-
如果原来这个点的
就 ,那么定向后出度只会小,所以一定在 以内。 -
如果大于,那么它只会连向
大于它的 的 。这样的 一定满足 ,不超过 个。
所以我们证明了每个点的出度是
模板题 Luogu P1989
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
pair <int,int> e[N];
vector <int> G[N];
int deg[N],vis[N],n,m;
inline int calc3()
{
static int vis[N];
memset(vis,0,sizeof(vis));
int ret = 0;
for(int i = 1;i <= n;i++)
{
for(auto to : G[i]) vis[to] = i;
for(auto to : G[i])
for(auto tw : G[to])
if(vis[tw] == i)
ret ++;
}
return ret;
}
int main()
{
cin>>n>>m;
for(int i = 1,x,y;i <= m;i++) cin>>x>>y,e[i] = make_pair(x,y),deg[x]++,deg[y]++;
for(int i = 1;i <= m;i++)
{
if(deg[e[i].first] > deg[e[i].second]) G[e[i].second].push_back(e[i].first);
else if(deg[e[i].first] < deg[e[i].second]) G[e[i].first].push_back(e[i].second);
else if(e[i].first < e[i].second) G[e[i].first].push_back(e[i].second);
else G[e[i].second].push_back(e[i].first);
}
cout<<calc3();
return 0;
}
还是向刚才一样赋一个属性值,这样一个四元环有
我们的基本思路是枚举两条边,将它们交汇在一起用桶统计。
由于一定有一个点被两条出边指向,我们考虑这样的思路,从一个点出发,走一条原图的无向边(新图可正可反),再走一条新图的有向边,走到
(
我们惊奇的发现,这样做正好能将上面的两种情况统计恰好
我们发现统计两次是因为一次从顶上出发,一次从下面出发,所以我们只需要统计时强制规定最后到达的点
时间复杂度
模板题 LOJ 191
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector <int> e[N],G[N];
int deg[N],n,m,pot[N];
inline bool lrg(int x,int y) // x > y ??
{
if(deg[x] ^ deg[y]) return deg[x] > deg[y];
else return x > y;
}
int main()
{
cin>>n>>m;
for(int i = 1,x,y;i <= m;i++) cin>>x>>y,e[x].push_back(y),e[y].push_back(x),deg[x]++,deg[y]++;
for(int i = 1;i <= n;i++)
for(auto to : e[i])
if(lrg(to,i))
G[i].push_back(to);
long long ans = 0;
for(int i = 1;i <= n;i++)
{
for(auto to : e[i])
for(auto tw : G[to])
if(lrg(tw,to) && lrg(tw,i))
ans += pot[tw],pot[tw]++;
for(auto to : e[i])
for(auto tw : G[to])
pot[tw] = 0;
}
cout<<ans;
return 0;
}
一道练习题:CF985G Team Players
给定
首先考虑容斥,答案等于
复杂度
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef unsigned long long ull;
vector <int> e[N],G[N];
int n,m,deg[N];
ull A,B,C;
inline bool lrg(int x,int y)
{
if(deg[x] ^ deg[y]) return deg[x] > deg[y];
return x > y;
}
inline ull calc0()
{
ull ret = 0;
for(int i = 0;i < n;i++)
{
if(i < n - 1) ret += 1ull * (n - i - 1) * (n - i - 2) / 2 * A * i;
ret += 1ull * i * (n - 1 - i) * B * i;
if(i > 0) ret += 1ull * i * (i - 1) / 2 * C * i;
}
return ret;
}
inline ull sum(int x) {if(x <= 0) return 0; return 1ull * (x + 1) * x / 2;}
inline ull calc1()
{
ull ret = 0;
for(int i = 0;i < n;i++)
for(auto to : e[i])
{
if(to < i) continue;
ret += (1ull * A * i + 1ull * B * to) * (n - 1 - to) + C * (sum(n - 1) - sum(to));
ret += (1ull * A * i + 1ull * C * to) * (to - i - 1) + B * (sum(to - 1) - sum(i));
ret += (1ull * B * i + 1ull * C * to) * i + A * sum(i - 1);
}
return ret;
}
inline ull calc2()
{
ull ret = 0;
for(int i = 0;i < n;i++)
{
ull sig1 = 0,cnt1 = 0,sig2 = 0,cnt2 = 0;
sort(e[i].begin(),e[i].end());
for(auto to : e[i])
{
if(to < i)
{
ret += sig1 + cnt1 * i * C + cnt1 * to * B;
cnt1++; sig1 += A * to;
}
else
{
ret += sig1 + cnt1 * i * B + cnt1 * to * C;
ret += sig2 + cnt2 * i * A + cnt2 * to * C;
cnt2++; sig2 += B * to;
}
}
}
return ret;
}
inline int min(int x,int y,int z) {return min(min(x,y),z);}
inline int max(int x,int y,int z) {return max(max(x,y),z);}
inline ull calc3()
{
static int vis[N];
memset(vis,-1,sizeof(vis));
ull ret = 0;
for(int i = 0;i < n;i++)
{
for(auto to : G[i]) vis[to] = i;
for(auto to : G[i])
for(auto tw : G[to])
if(vis[tw] == i)
ret += min(i,to,tw) * A + (i + to + tw - min(i,to,tw) - max(i,to,tw)) * B + max(i,to,tw) * C;
}
return ret;
}
int main()
{
cin>>n>>m;
cin>>A>>B>>C;
for(int i = 1,x,y;i <= m;i++)
{
cin>>x>>y;
e[x].push_back(y); e[y].push_back(x);
deg[x]++; deg[y]++;
}
for(int i = 0;i < n;i++)
for(auto to : e[i])
if(lrg(to,i))
G[i].push_back(to);
cout<<calc0() - calc1() + calc2() - calc3();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!