解题思路:
只是存一下带花树模板.。
#include<bits/stdc++.h>
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
if(c=='-')f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int N=505,M=300005;
int n,m,ans;
int tot,first[N],nxt[M],to[M];
int vt,fa[N],tp[N],pre[N],match[N],vis[N];
queue<int>q;
void add(int x,int y)
{
nxt[++tot]=first[x],first[x]=tot,to[tot]=y;
}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int lca(int x,int y)
{
++vt;
while(x)
{
if(vis[x]==vt)return x;
vis[x]=vt,x=find(pre[match[x]]);
if(!x)swap(x,y);
}
}
void group(int x,int y,int p)
{
while(find(x)!=p)
{
pre[x]=y,y=match[x];
if(tp[y]==2)tp[y]=1,q.push(y);
if(find(x)!=p)fa[x]=p;
if(find(y)!=p)fa[y]=p;
x=pre[y];
}
}
int bfs(int s)
{
for(int i=1;i<=n;i++)tp[i]=pre[i]=0,fa[i]=i;
while(!q.empty())q.pop();
q.push(s),tp[s]=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int e=first[u];e;e=nxt[e])
{
int v=to[e];
if(tp[v]==2||find(u)==find(v))continue;
if(!tp[v])
{
tp[v]=2,pre[v]=u;
if(!match[v])
{
for(int now=v,last,tmp;now;now=tmp)
{
last=pre[now],tmp=match[last];
match[now]=last,match[last]=now;
}
return true;
}
tp[match[v]]=1,q.push(match[v]);
}
else if(tp[v]==1)
{
int p=lca(u,v);
group(u,v,p),group(v,u,p);
}
}
}
return false;
}
int main()
{
//freopen("lx.in","r",stdin);
n=getint(),m=getint();
int x,y;
while(m--)
{
x=getint(),y=getint();
add(x,y),add(y,x);
}
for(int i=1;i<=n;i++)
if(!match[i])ans+=bfs(i);
printf("%d\n",ans);
for(int i=1;i<=n;i++)printf("%d ",match[i]);
return 0;
}