网上原理写的好的很多:
一般图最大匹配——带花树 - Mr_Spade - 博客园 (cnblogs.com)
(主席yyds)
(33条消息) 一般图最大匹配问题-带花树开花算法_Wo xi Meiz-CSDN博客
这里就贴个板子了......
uoj079
#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define sc second
#define pb push_back
#define ll long long
#define trav(v, x) for(auto v:x)
#define VI vector<int>
#define VLL vector<ll>
//define double long double
#define all(x) (x).begin(),(x).end()
using namespace std;
const double eps = 1e-10;//1e-12
const int N = 510;
const ll mod = 998244353;//1e9 + 7;
int n, m, a[N];
VI adj[N];
int match[N], fa[N], vis[N], pre[N];
queue<int>que;
int find(int x)
{return fa[x] == x ? x : fa[x] = find(fa[x]);}
int calc_lca(int x, int y)
{
static int buk[N];
static int nw;
++nw;
while(1)
{
if(x)
{
x = find(x);
if(buk[x] == nw)
return x;
buk[x] = nw;
x = pre[match[x]];
}
swap(x, y);
}
}
void shrink(int x, int y, int lca)
{
while(find(x) != lca)
{
pre[x] = y, y = match[x];
if(vis[y] == 2)
{
vis[y] = 1;
que.push(y);
}
if(find(x) == x)
fa[x] = lca;
if(find(y) == y)
fa[y] = lca;
x = pre[y];
}
}
bool aug(int s)
{
for(int i = 1; i <= n; i++)
fa[i] = i;
memset(vis, 0, sizeof vis);
memset(pre, 0, sizeof pre);
while(!que.empty())
que.pop();
vis[s] = 1;
que.push(s);
while(!que.empty())
{
int nw = que.front();
que.pop();
trav(nxt, adj[nw])
{
if(find(nw) == find(nxt) || vis[nxt] == 2)
continue;
if(!vis[nxt])
{
vis[nxt] = 2;
pre[nxt] = nw;
if(!match[nxt])
{
for(int x = nxt, y; x; x = y)
{
y = match[pre[x]];
match[x] = pre[x];
match[pre[x]] = x;
}
return 1;
}
vis[match[nxt]] = 1, que.push(match[nxt]);
}
else
{
int lca = calc_lca(nw, nxt);
shrink(nw, nxt, lca);
shrink(nxt, nw, lca);
}
}
}
return 0;
}
void sol()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
if(!match[i])
ans += aug(i);
}
cout << ans << '\n';
for(int i = 1; i <= n; i++)
cout << match[i] << ' ';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int tt;
tt = 1;
//cin >> tt;
while(tt--)
{
sol();
}
}