洛谷P3386 【模板】二分图匹配

匈牙利算法模板

 

 

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=1010;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
14     return x*f;
15 }
16 int mp[mxn][mxn];
17 int n,m,et;
18 int link[mxn];
19 bool vis[mxn];
20 bool DFS(int u){
21     for(int i=1;i<=n;i++){
22         if(!vis[i] && mp[u][i]){
23             vis[i]=1;
24             if(link[i]==-1 || DFS(link[i])){
25                 link[i]=u;
26                 return 1;
27             }
28         }
29     }
30     return 0;
31 }
32 int solve(){
33     int res=0;
34     for(int i=1;i<=n;i++){
35         memset(vis,0,sizeof vis);
36         if(DFS(i))res++;
37     }
38     return res;
39 }
40 int main(){
41     n=read();m=read();et=read();
42     int i,j;
43     int u,v;
44     for(i=1;i<=et;i++){
45         u=read();v=read();
46         if(u>m || v>m)continue;
47         mp[u][v]=mp[v][u]=1;
48     }
49     memset(link,-1,sizeof link);
50     int ans=solve();
51     printf("%d\n",ans);
52     return 0;
53 }

 

posted @ 2016-11-04 18:10  SilverNebula  阅读(329)  评论(0编辑  收藏  举报
AmazingCounters.com