二分圖ios
給定一個二分圖,結點個數分別爲n,m,邊數爲e,求二分圖最大匹配數算法
輸入格式:ide
第一行,n,m,espa
第二至e+1行,每行兩個正整數u,v,表示u,v有一條連邊code
輸出格式:blog
共一行,二分圖最大匹配ip
1 1 1 1 1
1
n,m<=1000,1<=u<=n,1<=v<=mci
由於數據有坑,可能會遇到v>m的狀況。請把v>m的數據自覺過濾掉。get
算法:二分圖匹配string
++++++++++++++++++++++++++++++++++++++++++++++++++++
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<cstdlib> 8 #include<iomanip> 9 #include<cassert> 10 #include<climits> 11 #include<vector> 12 #include<list> 13 #include<map> 14 #define maxn 1000001 15 #define F(i,j,k) for(int i=j;i<=k;i++) 16 #define M(a,b) memset(a,b,sizeof(a)) 17 #define FF(i,j,k) for(int i=j;i>=k;i--) 18 #define inf 0x7fffffff 19 #define maxm 2016 20 #define mod 1000000007 21 //#define LOCAL 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 int n1,n2,m; 30 int mp[maxm][maxm]; 31 int vis[maxn],link[maxn]; 32 inline bool find(int u) 33 { 34 F(i,1,n2){ 35 if(mp[u][i]&&!vis[i]){ 36 vis[i]=1; 37 if(link[i]==0||find(link[i])){ 38 link[i]=u; 39 return true; 40 } 41 } 42 } 43 return false; 44 } 45 int ans=0; 46 int main() 47 { 48 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 49 #ifdef LOCAL 50 freopen("data.in","r",stdin); 51 freopen("data.out","w",stdout); 52 #endif 53 cin>>n1>>n2>>m; 54 F(i,0,m-1){ 55 int x,y;cin>>x>>y; 56 mp[x][y]=1; 57 } 58 F(i,1,n1){ 59 M(vis,false); 60 if(find(i)) ans++; 61 } 62 cout<<ans<<endl; 63 return 0; 64 }