您的位置:首页 > 其它

带花树

2016-02-14 07:51 274 查看
带花树模板

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 605
#define M 200010
using namespace std;
int n,m,tot,head
,match
,fa
,flower
;
bool inflower
,vis
;
struct edge{int next,to;}e[M];
queue<int>q;
inline void add(int u,int v)
{
e[++tot]=(edge){head[u],v};
head[u]=tot;
e[++tot]=(edge){head[v],u};
head[v]=tot;
}
int lca(int u,int v)
{
int inpath
;
memset(inpath,0,sizeof(inpath));
while(1)
{
u=flower[u];
inpath[u]=1;
if(!match[u])break;
u=fa[match[u]];
}
while(1)
{
v=flower[v];
if(inpath[v])return v;
v=fa[match[v]];
}
}
void reset(int u,int anc)
{
while(u!=anc)
{
int v=match[u];
inflower[flower[u]]=1;
inflower[flower[v]]=1;
v=fa[v];
if(flower[v]!=anc)fa[v]=match[u];
u=v;
}
}
void bloom(int u,int v)
{
int t=lca(u,v);
memset(inflower,0,sizeof(inflower));
reset(u,t);reset(v,t);
if(flower[u]!=t)fa[u]=v;
if(flower[v]!=t)fa[v]=u;
for(int i=1;i<=n;i++)
if(inflower[flower[i]])
{
flower[i]=t;
if(!vis[i])
vis[i]=1,q.push(i);
}
}
bool path(int s)
{
memset(vis,0,sizeof(vis));
memset(fa,0,sizeof(fa));
for(int i=1;i<=n;i++)flower[i]=i;
while(!q.empty())q.pop();
q.push(s);vis[s]=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].next)
if(flower[u]!=flower[e[i].to]&&match[u]!=e[i].to)
{
if(e[i].to==s||(match[e[i].to]&&fa[match[e[i].to]]))
bloom(u,e[i].to);
else if(!fa[e[i].to])
{
fa[e[i].to]=u;
if(match[e[i].to])
{
q.push(match[e[i].to]);
vis[match[e[i].to]]=1;
}
else
{
u=e[i].to;
while(u)
{
match[u]=fa[u];
swap(u,match[fa[u]]);
}
return 1;
}
}
}
}
return 0;
}
void init()
{
int t,x,y;
scanf("%d%d%d",&n,&m,&t);
memset(head,0,sizeof(head));
memset(match,0,sizeof(match));
int ans=tot=0;
for(int i=1;i<=n;i++)
if(!match[i])ans+=path(i);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)init();
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: