带花树
2016-02-14 07:51
274 查看
带花树模板
View Code
#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
相关文章推荐
- 【BZOJ 1079】[SCOI2008]着色方案
- ubuntu下使用CGAL的pro文件
- 2月1日-2月13日,10小时,剩3185小时
- Linux系统编程基础知识 2015/1/14
- 【BZOJ 1070】[SCOI2007]修车
- 【BZOJ 1927】 [Sdoi2010]星际竞速
- 3*3卷积核实例
- 【BZOJ 1951】 [Sdoi2010]古代猪文
- 微信开发Demo
- IPV6地址数据库导出
- 【BZOJ 2005】[Noi2010]能量采集
- Androidx学习笔记(60)--- 监听应用的安装、卸载、更新状态
- Androidx学习笔记(59)--- 简易勒索软件原理
- Androidx学习笔记(58)--- 监控SD卡状态
- Androidx学习笔记(57)--- 短信拦截器
- Androidx学习笔记(56)--- IP拨号器
- Androidx学习笔记(55)--- 广播接收者
- Androidx学习笔记(54)--- Activity返回时传递数据
- KMP 算法
- Androidx学习笔记(53)--- 横竖屏切换的生命周期