二分图最大匹配模板
1 //洛谷3386 2 #include<bits/stdc++.h> 3 #define N 1005 4 using namespace std; 5 int n,m,s,tot,ans; 6 int head[N],match[N]; 7 bool vis[N]; 8 inline int read(){ 9 int x=0,f=1; char ch=getchar(); 10 while (ch>'9'||ch<'0'){ if (ch=='-') f=-1; ch=getchar(); } 11 while (ch<='9'&&ch>='0'){ x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } 12 return x*f; 13 } 14 struct node{ 15 int next,to; 16 }e[N*N]; 17 void add(int x,int y){ 18 e[++tot].next=head[x]; 19 head[x]=tot; 20 e[tot].to=y; 21 } 22 bool dfs(int u){ 23 for (int i=head[u];i;i=e[i].next){ 24 int v=e[i].to; 25 if (vis[v]) continue; 26 vis[v]=true; 27 if (!match[v]||dfs(match[v])){ 28 match[v]=u; 29 return true; 30 } 31 } 32 return false; 33 } 34 int main(){ 35 n=read(); m=read(); s=read(); 36 int x,y; 37 for (int i=1;i<=s;i++){ 38 scanf("%d%d",&x,&y); 39 if (x>n||y>m) continue; 40 add(x,y); 41 } 42 for (int i=1;i<=n;i++){ 43 memset(vis,0,sizeof(vis)); 44 if (dfs(i)) ans++; 45 } 46 printf("%d\n",ans); 47 return 0; 48 }