二分图的最大匹配(模板)

传送门:https://www.luogu.org/problem/P3386

已经加入极其简单的当前弧优化

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 int n,m,bian,s,t;
  4 int vis;
  5 int dep[6000001];
  6 int inque[6000001];
  7 int cur[6000001];
  8 int maxflow=0;
  9 struct edge{
 10     int to,nxt,flow;
 11 }e[6000001];int tot=-1;
 12 int first[6000001];
 13 const int inf=0x3f3f3f3f;
 14 inline int kd()
 15 {
 16     int x=0,f=1;char ch=getchar();
 17     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 18     while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
 19     return x*f;
 20 }
 21 inline void add_edge(int a,int b,int c)
 22 {
 23     e[++tot].to=b;
 24     e[tot].flow=c;
 25     e[tot].nxt=first[a];
 26     first[a]=tot;
 27 }
 28 bool bfs()
 29 {
 30     for(register int i=0;i<=n+m+2;i++)cur[i]=first[i],inque[i]=false,dep[i]=0x3f3f3f3f;
 31     dep[s]=0;
 32     queue<int>q;
 33     q.push(s);
 34     while(q.empty()==false)
 35     {
 36         int now=q.front();
 37         q.pop();
 38         inque[now]=false;
 39         for(int i=first[now];i!=-1;i=e[i].nxt)
 40         {
 41             int to=e[i].to;
 42             if(dep[now]+1<dep[to]&&e[i].flow!=0)
 43             {
 44                 dep[to]=dep[now]+1;
 45                 if(inque[to]==false)
 46                 {
 47                     inque[to]=true;
 48                     q.push(to);
 49                 }
 50             }
 51         }
 52     }
 53     if(dep[t]==0x3f3f3f3f)return false;
 54     return true;
 55 }
 56 int dfsl(int now,int nowflow)
 57 {
 58     int rlow=0;
 59     if(now==t)
 60     {
 61         vis=1;
 62         maxflow+=nowflow;
 63         return nowflow;
 64     }
 65     int used=0;
 66     for(int i=cur[now];i!=-1;i=e[i].nxt)
 67     {
 68         cur[now]=i;
 69         if(dep[now]+1==dep[e[i].to]&&e[i].flow!=0)
 70         {
 71             if(rlow=dfsl(e[i].to,min(nowflow,e[i].flow)))
 72             {
 73                 used+=rlow;
 74                 e[i].flow-=rlow;
 75                 e[i^1].flow+=rlow;
 76                 if(used==nowflow)return nowflow;
 77             }
 78         }
 79     }
 80     if(used==0)dep[now]=0x3f3f3f3f;
 81     return used;
 82 }
 83 void dinic()
 84 {
 85     while(bfs())
 86     {
 87         vis=1;
 88         while(vis==1)
 89         {
 90             vis=0;
 91             dfsl(s,inf);
 92         }
 93     }
 94     return;
 95 }
 96 int main()
 97 {
 98     memset(first,-1,sizeof(first));
 99     n=kd(),m=kd(),bian=kd(),s=0,t=m+n+1;
100     for(int i=1;i<=n;i++)
101     {
102         add_edge(s,i,1);
103         add_edge(i,s,0);
104     }
105     for(int i=1;i<=bian;i++)
106     {
107         int a=kd(),b=kd();
108         if(a>n||b>m)continue;
109         add_edge(a,b+n,1);
110         add_edge(b+n,a,0);
111     }
112     for(int i=1;i<=m;i++)
113     {
114         add_edge(i+n,t,1);
115         add_edge(t,i+n,0);
116     }
117     dinic();
118     cout<<maxflow<<endl;
119 }

猜你喜欢

转载自www.cnblogs.com/1129-tangqiyuan/p/11800136.html