P6113-[模板]一般图最大匹配【带花树】
2024-09-02 19:15:07
正题
题目链接:https://www.luogu.com.cn/problem/P6113
题目大意
给出一张无向图,求最大匹配。
\(1\leq n\leq 10^3,1\leq m\leq 5\times 10^4\)
解题思路
带花树的模板,我也不会讲/kel
所以看下面两篇大佬的博客吧
yyb-带花树算法学习笔记
Bill Yang-带花树学习笔记
时间复杂度好像是\(O(n^3)\)的
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1010,M=3e5+10;
struct node{
int to,next;
}a[M];
int n,m,tot,tk,ans,ls[N],dfn[N];
int fa[N],pre[N],tag[N],match[N];
queue<int> q;
void addl(int x,int y){
a[++tot].to=y;
a[tot].next=ls[x];
ls[x]=tot;return;
}
int find(int x)
{return (fa[x]==x)?x:(fa[x]=find(fa[x]));}
int LCA(int x,int y){
++tk;x=find(x);y=find(y);
while(dfn[x]!=tk){
dfn[x]=tk;
x=find(pre[match[x]]);
if(y)swap(x,y);
}
return x;
}
void Blossom(int x,int y,int lca){
while(find(x)!=lca){
pre[x]=y;y=match[x];
if(tag[y]==2){tag[y]=1;q.push(y);}
fa[x]=fa[y]=lca;x=pre[y];
}
return;
}
int Aug(int s){
memset(tag,0,sizeof(tag));
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++)fa[i]=i;
while(!q.empty())q.pop();
q.push(s);tag[s]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(!tag[y]){
tag[y]=2;pre[y]=x;
if(!match[y]){
for(int u=y,lst;u;u=lst)
lst=match[pre[u]],match[u]=pre[u],match[pre[u]]=u;
return 1;
}
tag[match[y]]=1;q.push(match[y]);
}
else if(tag[y]==1&&find(y)!=find(x)){
int lca=LCA(x,y);
Blossom(x,y,lca);
Blossom(y,x,lca);
}
}
}
return 0;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
addl(x,y);addl(y,x);
}
for(int i=1;i<=n;i++)
if(!match[i])ans+=Aug(i);
printf("%d\n",ans);
for(int i=1;i<=n;i++)
printf("%d ",match[i]);
return 0;
}
最新文章
- php token的生成
- Chrome清除dns缓存
- Array与ArrayBuffer
- Java第一次实验
- SpringMVC+MyBatis+EasyUI 实现分页查询
- 服务器 CentOS上yum安装Nginx服务
- Ubuntu Server搭建svn服务以及迁移方法【转】
- return View()
- php多层数组与对象的转换实例代码
- WPF——绑定数据库数据(Listview)
- C# DateTime.Now
- nginx + uwsgi 部署 Django+Vue项目
- js判断上传图片文件大小,尺寸,格式
- ACPI:Memory错误解决办法
- intellij idea (Android studio )外部程序 打开某扩展名(格式)
- echarts中国地图坐标弹框
- Sql Server中的游标最好只用于有主键或唯一键的表
- 前端学习 -- Css -- 默认样式
- nginx访问日志,错误日志参数说明
- HTTP协议图--HTTP 协议基础