正题

题目链接: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;
}

最新文章

  1. php token的生成
  2. Chrome清除dns缓存
  3. Array与ArrayBuffer
  4. Java第一次实验
  5. SpringMVC+MyBatis+EasyUI 实现分页查询
  6. 服务器 CentOS上yum安装Nginx服务
  7. Ubuntu Server搭建svn服务以及迁移方法【转】
  8. return View()
  9. php多层数组与对象的转换实例代码
  10. WPF——绑定数据库数据(Listview)
  11. C# DateTime.Now
  12. nginx + uwsgi 部署 Django+Vue项目
  13. js判断上传图片文件大小,尺寸,格式
  14. ACPI:Memory错误解决办法
  15. intellij idea (Android studio )外部程序 打开某扩展名(格式)
  16. echarts中国地图坐标弹框
  17. Sql Server中的游标最好只用于有主键或唯一键的表
  18. 前端学习 -- Css -- 默认样式
  19. nginx访问日志,错误日志参数说明
  20. HTTP协议图--HTTP 协议基础

热门文章

  1. mysql悲观锁和乐观锁
  2. uwp之图片旋转动画实现
  3. mycat 分片的策略
  4. Qt Model/View(模型/视图)结构(无师自通)
  5. 防止XSS 攻击集成springboot
  6. 关于Junit中Assert已经过时
  7. 设计模式&lt;一&gt;
  8. Spring Boot集成Redis集群(Cluster模式)
  9. Qt5-调试器安装
  10. 干货:详解C++ sort函数的cmp参数!