Luogu 3386(二分图最大匹配)

来源:互联网 发布:属性数据分析 编辑:程序博客网 时间:2024/05/29 09:34

传送门

模板题。

新技能get:存增广标记的cov数组如果使用int类型就不用每次memset(以前的版本在之前的博文中有过),可以大幅节约时间。

晚安~微笑

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>using namespace std;const int N=1004;int n,m,eg;int head[N<<1],etot=0;struct EDGE {int v,nxt;}e[N*N];int cov[N<<1],lk[N<<1];inline int read() {int x=0;char c=getchar();while (c<'0'||c>'9') c=getchar();while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();return x;}inline void adde(int u,int v) {e[etot].nxt=head[u],e[etot].v=v,head[u]=etot++;e[etot].nxt=head[v],e[etot].v=u,head[v]=etot++;}bool dfs(int p,int id) {for (int i=head[p];~i;i=e[i].nxt) {int v=e[i].v;if (cov[v]^id) {cov[v]=id;if (lk[v]==-1||dfs(lk[v],id)) {lk[v]=p;return true;}}}return false;}int main() {//freopen("P3386.in","r",stdin);memset(head,-1,sizeof(head));n=read(),m=read(),eg=read();for (register int i=0;i<eg;++i) {int u=read(),v=read();if (u>n||v>m) continue;adde(u,v+n);}memset(lk,-1,sizeof(lk));int max_match(0);for (int i=1;i<=n;++i)if (dfs(i,i)) ++max_match;cout<<max_match<<endl;return 0;}


原创粉丝点击