Rogu P3386 [TEMPLATE] Bipartite Graph Matching Dinic version, p31_dinic

Source: Internet
Author: User

Rogu P3386 [TEMPLATE] Bipartite Graph Matching Dinic version, p31_dinic
Background

Bipartite Graph

Description

Given a bipartite graph, the number of nodes is n, m, and the number of edges is e.

Input/Output Format

Input Format:

 

First line, n, m, e

The second to e + 1 rows have two positive integers u and v, indicating that u and v have a connected edge.

 

Output Format:

 

A total of rows, maximum matching of bipartite graphs

 

Input and Output sample input sample #1: Copy
1 1 11 1
Output example #1: Copy
1
Description

N, m \ leq 1000n, m ≤ 1000, 1 \ leq u \ leq n1 ≤ u ≤ n, 1 \ leq v \ leq m1 ≤ v ≤ m

Due to Data Pitfalls, v> mv> m may occur. Please filter out the v> mv> m data.

Algorithm: Bipartite Graph Matching

 

Create a super source node S

Create a super sink T

Edge connecting n S to 1-n

Connect m edge n + 1-n + 1 + m to T

The weight of all edges is 1.

Maximum stream running

 

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 using namespace std; 7 const int MAXN=3*1e6; 8 const int INF=0x7ffff; 9 inline int read()10 {11     char c=getchar();int flag=1,x=0;12     while(c<'0'||c>'9')    {if(c=='-')    flag=-1;c=getchar();}13     while(c>='0'&&c<='9')     x=x*10+c-48,c=getchar();return x*flag;14 }15 struct node16 {17     int u,v,f,nxt;18 }edge[MAXN];19 int head[MAXN];20 int num=0;21 inline void add_edge(int x,int y,int z)22 {23     edge[num].u=x;24     edge[num].v=y;25     edge[num].f=z;26     edge[num].nxt=head[x];27     head[x]=num++;28 }29 int n,m,e;30 int S,T;31 int deep[MAXN],cur[MAXN];32 inline bool BFS()33 {34     memset(deep,0,sizeof(deep));35     deep[S]=1;36     queue<int>q;37     q.push(S);38     while(q.size()!=0)39     {40         int p=q.front();q.pop();41         for(int i=head[p];i!=-1;i=edge[i].nxt)42             if(deep[edge[i].v]==0&&edge[i].f)43                 deep[edge[i].v]=deep[p]+1,q.push(edge[i].v);44     }45     return deep[T];46 }47 int DFS(int now,int nowflow)48 {49     if(nowflow<=0||now==T)    return nowflow;50     int totflow=0;51     for(int &i=cur[now];i!=-1;i=edge[i].nxt)52     {53         if(deep[edge[i].v]==deep[now]+1&&edge[i].f)54         {55             int canflow=DFS(edge[i].v,min(edge[i].f,nowflow));56             totflow+=canflow;57             nowflow-=canflow;58             edge[i].f-=canflow;59             edge[i^1].f+=canflow;    60             if(nowflow<=0)    break;61         }62     }63     return totflow;64 }65 int ans=0;66 inline void Dinic()67 {68     while(BFS())69     {70         memcpy(cur,head,sizeof(head));71         ans+=DFS(S,INF);72     }73         74 }75 int main()76 {77     memset(head,-1,sizeof(head));78     n=read();m=read();e=read();79     for(int i=1;i<=e;i++)80     {81         int x=read(),y=read();82         add_edge(x,y+n,1);83         add_edge(y+n,x,0);84     }85     S=0,T=INF;86     for(int i=1;i<=n;i++)87         add_edge(S,i,1),add_edge(i,S,0);88     for(int i=1;i<=m;i++)89         add_edge(i+n,T,1),add_edge(T,i+n,0);90     Dinic();91     printf("%d",ans);92     return 0;93 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.